在我对Angular的探索中,我发现了两种可能的方法来在另一个模块中使用一个模块.
(使用angular-express-starter project作为参考)
>方法1:
在进口数组中声明它. For example
@NgModule({ declarations: [ AppComponent ],imports: [ BrowserModule,SharedModule,FormsModule ] })
>方法2:
使用loadChildrenin路由. For example:
export const routes: Route[] = [ { path: '',pathMatch: 'full',redirectTo: 'weather'},{ loadChildren: 'app/dashboard/dashboard.module#DashboardModule',path: 'dashboard' },{ loadChildren: 'app/profile/profile.module#ProfileModule',path: 'profile' },{ loadChildren: 'app/weather/weather.module#WeatherModule',path: 'weather' } ];
这两种方法有什么实际差异?
解决方法
What are the practical differences between these two methods?
最大的区别是通过loadChildren加载的模块将拥有自己的注入器,而来自导入模块的提供程序将合并到一个根注入器中.这意味着您无法将延迟加载模块中的提供程序注入其他延迟加载的模块中.
其他差异:
>如果不使用路由,则无法使用loadChildren
>只有在导航到相应路径时才会加载通过loadChildren加载的模块
有关更多信息,请阅读