Angular 2应用程序结构/核心模块和第三方库
我试图找到一个好方法来构造我的Angular 2应用程序。Angular 2 styleguide建议创建核心模块。如果我理解正确的话,核心模块的目标是收集一次性使用的类和组件,并保持根模块的纤细。还有一篇文章写道,我应该将资产所需的所有模块导入到核心模块中 对于需要包含在forRoot()方法中的第三方库(如NgBootstrap或angular2通知),我有点困惑。通常,forRoot()方法只应在根模块中调用。我应该在根模块中还是在核心模块中包含这样的模块 在下面的示例中,我需要对angular2通知进行一些配置。为了保持我的根模块苗条,我在核心模块中导入了SimpleNotificationsAngular 2应用程序结构/核心模块和第三方库,angular,angular2-modules,angular2-styleguide,Angular,Angular2 Modules,Angular2 Styleguide,我试图找到一个好方法来构造我的Angular 2应用程序。Angular 2 styleguide建议创建核心模块。如果我理解正确的话,核心模块的目标是收集一次性使用的类和组件,并保持根模块的纤细。还有一篇文章写道,我应该将资产所需的所有模块导入到核心模块中 对于需要包含在forRoot()方法中的第三方库(如NgBootstrap或angular2通知),我有点困惑。通常,forRoot()方法只应在根模块中调用。我应该在根模块中还是在核心模块中包含这样的模块 在下面的示例中,我需要对angu
- 这是正确的方法吗?要使应用程序正常工作,我仍然需要在应用程序模块中导入SimpleNotificationsModule.forRoot()
- 为什么需要在核心模块中再次导入SimpleNotificationsModule。它不应该由应用程序模块提供吗。我想多亏了forRoot(),他们的核心模块使用了与其他应用程序模块相同的导入模块
- 如果是,是否应该在核心模块内导入SimpleNotifications?我真的应该在那里调用forRoot()方法吗
应用程序模块
...
import {SimpleNotificationsModule} from 'angular2-notifications';
@NgModule({
declarations: [...],
imports: [
BrowserModule,
CoreModule,
NgbModule.forRoot(),
SimpleNotificationsModule.forRoot(),
],
bootstrap: [AppComponent]
})
export class AppModule {}
应用程序组件
...
<notifications></notifications>
通知组件
...
<notifications></notifications>
import {Component, ViewEncapsulation} from '@angular/core';
@Component({
selector: 'notifications',
template: `<simple-notifications [options]="notificationOptions">
</simple-notifications>`,
styleUrls: ['./notifications.component.css'],
encapsulation: ViewEncapsulation.None
})
export class NotificationsComponent {
public notificationOptions = {
position: ['top', 'right'],
timeOut: 5000,
maxStack: 5,
lastOnBottom: true
};
}
从'@angular/core'导入{Component,ViewEncapsulation};
@组成部分({
选择器:“通知”,
模板:`
`,
样式URL:['./notifications.component.css'],
封装:视图封装。无
})
导出类通知组件{
公共通知选项={
位置:[“顶部”、“右侧”],
超时:5000,
maxStack:5,
拉斯顿·巴顿:没错
};
}
.forRoot()用于注册模块中服务的提供程序
- 如果需要引用,请导入不带forRoot()的模块 模块中的组件/指令或模型。在应用程序中导入模块的时间没有限制
- 如果需要从模块提供服务,请使用forRoot()导入模块。通常,我们以这种方式导入模块,只有那些将服务作为单例使用的模块