angular – 在模块中使用另一个模块:在路由中导入vs使用loadChildren

前端之家收集整理的这篇文章主要介绍了angular – 在模块中使用另一个模块:在路由中导入vs使用loadChildren前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我对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加载的模块

有关更多信息,请阅读

> Avoiding common confusions with modules in Angular

猜你在找的Angularjs相关文章