我刚刚升级到angular2 RC5并将这篇文章改为红色@NgModules
https://angularjs.blogspot.de/2016/08/angular-2-rc5-ngmodules-lazy-loading.html
使用@NgModules
组件,指令,服务等在模块根级别上声明。我的想法是:我们不是试图解耦思考而不是我们试图归档那些甚至可以被它自己使用的构建组件吗?
如果我现在有一个组件,例如“NoteCard”,它用在“容器” - 组件“Notes”中。
NoteCard:
import {Component} from "@angular/core";
@Component({
selector: 'note-card',
styleUrls: ['app/ui/note-card/note-card.css'],
templateUrl: 'app/ui/note-card/note-card.html'
})
export class NoteCard {}
注意:
import {Component} from "@angular/core";
import {NoteCard, NoteCreator} from "../../ui";
import {NotesService} from "../../services";
@Component({
selector: 'notes-container',
directives: [
NoteCard
],
styleUrls: ['app/containers/notes/notes.css'],
templateUrl: 'app/containers/notes/notes.html'
})
export class Notes {}
在RC4中,我们只需将“NoteCard”注入“Notes”。通过这个我们可以单独使用“NoteCard”(当然它没有依赖关系)和“Notes”与“NoteCard”结合使用,因为它是它自己的依赖。
使用RC5我必须将“NoteCard”注入@NgModule
根级别。有了它,我总是需要一个额外的级别,如果它变得更复杂,它可能会高于注入依赖项的几个级别。
在项目中使用多个@NgModule
是否有意义?在我的情况下,每个“容器”组件应该是@NgModule
吗?
答案 0 :(得分:1)
确保拥有多个模块是有意义的!否则你不会使用这个新功能的好处。
请阅读这篇文章:https://angular.io/docs/ts/latest/guide/ngmodule.html
它很长,是的..但是有很多例子,代码片段和掠夺者。
在现场演示中,您将看到不同的模块,有时内部只有一个组件,有时会有更多组件。并确定哪些组件可见(导出)到模块外部。