Angular子模块未从父模块看到类

时间:2019-05-10 19:22:31

标签: angular ng-modules

我有一个角度配方模块。它声明并导出配方组件。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RecipeComponent } from './recipe/recipe.component';

@NgModule({
  declarations: [
    RecipeComponent
  ],
  imports: [
    CommonModule
  ],
  exports: [
      RecipeComponent
  ]
})
export class RecipeModule { }

配方模块结构还包含一个配方(多个)页面,该页面将列出多个配方。但是,当我将食谱组件放入食谱页面html标记内时,出现错误

core.js:15724 ERROR Error: Uncaught (in promise): Error: Template parse errors:
'app-recipe' is not a known element:
1. If 'app-recipe' is an Angular component, then verify that it is part of this module.
2. If 'app-recipe' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
<ion-content>

除非我将RecipeModule包含在页面子目录内的recipes.module导入中(注释掉的行会产生错误,注释掉它们会使错误消失)

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';
//import { RecipeModule } from '../../recipe/recipe.module';

import { IonicModule } from '@ionic/angular';

import { RecipesPage } from './recipes.page';

const routes: Routes = [
  {
    path: '',
    component: RecipesPage
  }
];

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
//    RecipeModule,
    RouterModule.forChild(routes)
  ],
  declarations: [RecipesPage]
})
export class RecipesPageModule {}

在我看来,在子模块中包含一个父模块似乎是一种递归包含。这是正确的做法,还是会产生副作用?有更好的方法吗?

文件夹结构:

    [-] --- src
         |
        [-] -- app
             |
            [-] -- recipe
                 |
                [-] -- recipe
                    |
                    ( the recipe component files)
                 |
                [-] -- recipes
                    |
                    (recipe page with its own module}
                 |
                -- recipe.module.ts
             |
             -- app.module.ts

0 个答案:

没有答案