我是 Angular2 新手,我在 im(ex) 移植模块方面遇到了一些困难。
根据官方样式指南 CoreModule 是:
CoreModule - import once when the app starts and never import anywhere else.
这是否意味着所有从 Core 模块导出的内容将仅在仅导入它的模块中可用?有没有办法让这些东西在某些子模块中可用?我创建了虚拟项目只是为了测试这种行为:
虚拟组件:
@Component({
selector: 'dummy-component',
template: '<h4>Dummy component</h4>',
})
export class DummyComponent {}
核心模块:
import { DummyComponent } from './dummy.component';
@NgModule({
declarations : [DummyComponent],
exports : [DummyComponent]
})
export class CoreModule {}
应用模块(根模块):
@NgModule({
declarations: [AppComponent,],
imports: [BrowserModule, CoreModule, HomeModule, RoutingModule],
exports : [CoreModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
主页模块声明主页组件:
@Component({
selector: 'home-component',
template:
`
<h2>Home component</h2>
<dummy-component></dummy-component>
`
})
export class HomeComponent {}
当我尝试运行此代码时出现此错误:
'dummy-component' is not a known element
最佳答案
如果你想要其他模块中的东西,那么把它放到一个共享模块(功能模块)中。 CoreModule 不是正确的地方。
是的,您需要将一个模块导入到您想要使用该模块中的组件、指令或管道的每个模块。
关于angular - 使用核心模块中的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42145705/