等效于Angular的APP_INITIALIZER的模块 [英] Module equivalent of APP_INITIALIZER in Angular

查看:38
本文介绍了等效于Angular的APP_INITIALIZER的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

功能模块的 APP_INITIALIZER 等效于什么在角?当我的功能模块(延迟)加载时,我想运行一些代码.该代码将需要注入一些依赖项.

What is the equivalent of the APP_INITIALIZER for feature modules in Angular? I want to run some code when my feature module is (lazily) loaded. That code will want some dependencies injected.

虽然我似乎找不到任何类似的模块项目.我曾在Angular docs网站(初始化程序")上使用过搜索,但一无所获.我浏览了模块文档的各个部分,并在浏览器中使用了类似的搜索词.但是我什么都没找到.

I can't seem to find any similar item for modules though. I've used the Search on the Angular docs website ("initializer") but found nothing. I've gone through parts of the modules documentation and used similar search terms in-browser. But I haven't found anything.

如何在Angular中的模块加载上运行代码(取决于通过DI的共享服务)?

How can you run code (depending on shared services via DI) on module load in Angular?

PS.可能这是一个XY问题,其中"Y"是我上面的问题.此时,我对学习"Y"本来就很感兴趣,但是为了完整起见,我的"X"是我希望功能模块回调共享模块中的 MenuService 以注册任何内容.功能模块想要贡献的菜单项.

PS. Possibly this is an XY-problem, where "Y" is my question above. At this point I'm intrinsically interested in learning about "Y", but for completeness, my "X" is that I want my feature module to call back to a MenuService in the shared module to register any menu items that feature module wants to contribute.

推荐答案

不知道它是否惯用,但我建议使用模块 constructor 函数来回答我的问题.例如:

Not sure if it's idiomatic, but I'd answer my question by suggesting to use the module constructor function for this purpose. So for example:

@NgModule({
  imports: [
    CommonModule,
    ThingsRoutingModule,
  ],
  declarations: [
    // Feature module components here
  ],
})
export class ThingsModule {
  constructor(myService: MySharedService) {
    myService.doSomething('From lazy loaded feature module!');
  }
}

因此在其中插入了 MySharedService .

这篇关于等效于Angular的APP_INITIALIZER的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆