Javascript 调用forRoot时角度延迟加载不起作用
我有一个延迟加载模块,它需要公开提供程序,因此我使用Javascript 调用forRoot时角度延迟加载不起作用,javascript,angular,Javascript,Angular,我有一个延迟加载模块,它需要公开提供程序,因此我使用forRoot约定并返回以下代码: @NgModule({ imports: [RouterModule.forChild([ {path: "", component: LazyComponent}, ])], declarations: [LazyComponent], }) export class LazyModule { static forRoot() { return { ngModul
forRoot
约定并返回以下代码:
@NgModule({
imports: [RouterModule.forChild([
{path: "", component: LazyComponent},
])],
declarations: [LazyComponent],
})
export class LazyModule {
static forRoot() {
return {
ngModule: LazyModule,
providers: [provider]
};
}
}
问题是,当我在我的应用程序模块中调用forRoot时,延迟加载不再工作。(我在控制台中看不到单独的块)
@NgModule({
声明:[
应用组件,
Hello组件
],
进口:[
浏览器模块,
批准模块,
LazyModule.forRoot()当您在AppModule
imports数组中导入LazyModule
时,它不再是“惰性”模块。惰性模块应仅在专用RoutingModule
中引用
所以,如果我理解正确,您愿意在LazyModules之间共享服务吗
如果是这样,请从AppModule
中删除LazyModule
,创建一个SharedModule,并将您想要共享的服务移动到SharedModule中的提供者数组中。使用forRoot
在AppModule
中导入SharedModule,然后在L中不使用forRoot
导入您的SharedModule
azyModules当我们将模块定义为Lazy RouterModule.forChild时,请尝试以下代码:
@NgModule({
imports: [RouterModule.forChild([
{path: "", component: LazyComponent},
])],
declarations: [LazyComponent],
})
export class LazyModule { }
但在加载父模块时,请尝试以下代码:
@NgModule({
declarations: [
AppComponent,
HelloComponent
],
imports: [
BrowserModule,
AppRoutingModule,
RouterModule.forRoot([
{
path: '',
loadChildren: './layout/layout.module#LayoutModule',
canActivate: [AuthGuard]
}
]) <======== right way to load lazy module
],
bootstrap: [AppComponent]
})
export class AppModule {
}
@NgModule({
声明:[
应用组件,
Hello组件
],
进口:[
浏览器模块,
批准模块,
RouterModule.forRoot([
{
路径:“”,
loadChildren:“./layout/layout.module#LayoutModule”,
canActivate:[AuthGuard]
}
])从现在起,不可能执行for root
(或任何其他此类配置静态方法)在一个将延迟加载的模块中。这里的问题是,这样一个方法返回一个带有提供者的ModuleWithProviders
,而loadChildren需要一个NgModuleFactory
我遇到了一个类似的问题,我不得不延迟加载带有一些配置的模块。因此,我想出了这个临时修复方法
惰性模块
父模块
因此,我只是在修改由@NgModule
装饰器创建的注释。在这个阶段,Angular不支持使用返回ModuleWithProviders
的静态方法进行延迟加载。如果您在渴望模块中导入LazyLoad模块,那么webpack将不会为其创建块,因为您的意思是我不能你可以创建一个单独的模块,只包含提供程序并导入它。这并不能回答他的问题。这里提到的forRoot在哪里?
@NgModule({
declarations: [
AppComponent,
HelloComponent
],
imports: [
BrowserModule,
AppRoutingModule,
RouterModule.forRoot([
{
path: '',
loadChildren: './layout/layout.module#LayoutModule',
canActivate: [AuthGuard]
}
]) <======== right way to load lazy module
],
bootstrap: [AppComponent]
})
export class AppModule {
}
@NgModule({
imports: [
RouterModule.forChild([
{path: "", component: LazyComponent},
])],
declarations: [LazyComponent],
})
export class LazyModule {
// considering the function only handle providers
static setDynamicProviders(provider?: any) {
if(LazyModule && LazyModule.__annotations__ && LazyModule.__annotations__[0]){
LazyModule.__annotations__[0].providers.push(provider);
}
return LazyModule;
}
}
const someService = {
provide: 'SOME_TOKEN',
useFactory: (dependecy1) => (//dome something with dependency1)
deps: [SOME_DEPENDENCY_TOKEN]
}
@NgModule({
imports: [
RouterModule.forRoot([
path: 'lazy',
loadChildren: ()=>(import('*your path to lazy module*').then(ref => ref.LazyModule.setDynamicProvider(someService)))
])
]
})