调用forRoot时,角延迟加载不起作用 [英] Angular lazy load does not work when invoking forRoot
问题描述
我有一个需要加载提供程序的惰性加载模块,因此我正在使用 forRoot
约定并返回以下代码:
I have a lazy load module which needs to expose providers, so I am using the forRoot
convention and returning the following code:
@NgModule({
imports: [RouterModule.forChild([
{path: "", component: LazyComponent},
])],
declarations: [LazyComponent],
})
export class LazyModule {
static forRoot() {
return {
ngModule: LazyModule,
providers: [provider]
};
}
}
问题是当我在我的应用模块中调用forRoot时,延迟加载不再起作用.(我在控制台中看不到单独的块)
The problem is when I invoke the forRoot in my app module the lazy load does not work anymore. ( I don't see the separate chunk in my console )
@NgModule({
declarations: [
AppComponent,
HelloComponent
],
imports: [
BrowserModule,
AppRoutingModule,
LazyModule.forRoot() <======== this stops the lazy load module
],
bootstrap: [AppComponent]
})
export class AppModule {
}
据我了解,它只能使提供程序成为单例,为什么它不起作用?
From what I learned it should only make the providers singleton, why it does not work?
推荐答案
截至目前,不可能在模块中执行 forRoot
(或此类的其他任何配置静态方法)会延迟加载.这里的问题是,这样的方法会返回 ModuleWithProviders
,而loadChildren需要一个 NgModuleFactory
.
As of right now, it's not possible to execute a forRoot
(or any other configuration static method of this sort) in a module that will load lazily. The problem here is that such a method returns a ModuleWithProviders
while loadChildren requires a NgModuleFactory
.
这篇关于调用forRoot时,角延迟加载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!