在 AppModule 中订阅 pipes/directives 不失懒惰

Subscribe pipes/directives in AppModule without losing lazyness

我刚刚将我的应用程序更新到 RC6,我注意到他们已经弃用了 @Componenet.pipes 和 @Component.directives。

他们建议将它们导入模块 class:

@NgModule({
  imports: [ BrowserModule ],
  declarations: [
    AppComponent,
    HighlightDirective
  ],
  bootstrap: [ AppComponent ]
})

我遵循了 Angular 风格指南中建议的项目结构,所以我有一些懒惰的文件夹(例如 +SubscriptionPage/)

问题是在我的“+SubscriptionPage/”文件夹中我有一些 pipes/directives 我只在应用程序的那部分使用。 因此,如果我将 pipes/directives 放在 RootModule 中,它们将以急切的方式加载,而不是以懒惰的方式加载。

"Section Specific"pipes/directives又不失懒惰的正确使用方法是什么

非常感谢

您应该为此创建一个惰性模块: (摘自 here

NgModules enable a simple way to lazy load pieces of your application via the router. A simple example illustrates this:

import {RouterModule} from ‘@angular/router’
import {NgModule} from ‘@angular/core’
@NgModule({
  declarations: [ MyComponent, MyHomeRoute ],
  bootstrap: [ MyComponent ],
  imports: [
    RouterModule.forRoot([
      { path: ‘home’, component: MyHomeRoute },
      { path: ‘lazy’, loadChildren: ‘./my-lazy-module’ }
    ])
})
class MyAppModule {}

You simply define a loadChildren property on a route, and Angular will go fetch the module at that location and load the routes defined in it into the router config.

import {RouterModule} from ‘@angular/router’
import {NgModule} from ‘@angular/core’

@NgModule({
  declarations: [ MyLazyHome, MyLazyChild ],
  imports: [
    RouterModule.forChild([
      { path: ‘’, component: MyLazyHome },
      { path: ‘a’, component: MyLazyChild }
    ])
  ]
})
class MyLazyModule {}