角度APP_INITIALIZER是否在延迟加载的模块内部工作 [英] Angular Does APP_INITIALIZER work inside of lazy loaded modules
问题描述
我有一个延迟加载的模块,正在尝试添加APP_INITIALIZER,但未触发.我有与我的主应用程序完全相同的语法,并且可以按预期工作.延迟加载的模块会触发APP_INITIALIZER吗?
I have a lazy loaded module that I'm trying to add APP_INITIALIZER but its not firing. I have the exact same syntax as my main app where its working as expected. Does a lazy loaded module fire the APP_INITIALIZER?
推荐答案
不幸的是,在延迟加载的模块中未调用APP_INITIALIZER,因为该应用程序之前已被初始化.
Unfortunately APP_INITIALIZER is not called in a lazy loaded module, because the application has already been initialized before.
您现在可以做什么:
您可以简单地利用模块的构造函数,该函数在模块初始化后立即被调用,并由注入器进行充分处理:
You may simply utilize the module's constructor, which is called as soon as the module gets initialized, and gets full treatment by the injector:
@NgModule({
...
})
export class MyModule {
constructor( <INJECTIONS> ) {
console.log('Module initialized');
}
}
此方法有两个局限性:
- 您只能在内部使用同步呼叫
- 调用构造函数时,模块尚未初始化,因此,例如,您不能动态地向此处定义的组件添加路由(可悲的是,我想做的事)
将来可能会有所帮助:
在GitHub上正在进行的讨论关于引入MODULE_INITIALIZER在模块初始化后会调用它,这将解决这些限制.
There is an ongoing discussion on GitHub about introducing a MODULE_INITIALIZER that gets called after module initialization, which would solve these limitations.
这篇关于角度APP_INITIALIZER是否在延迟加载的模块内部工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!