Angular2 RC5跨模块提供程序/扫描 [英] Angular2 RC5 Cross-Module Provider / Scanning

查看:78
本文介绍了Angular2 RC5跨模块提供程序/扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个当前最新的Angular RC5和Material Design的项目.并非最后一部分是相关的.

I'm working on a project which is currently Angular RC5 and Material Design latest. Not that the last part is relevant.

由于明显的原因,我开始创建单独的模块,并放弃将所有内容都包含在主模块中.但是,我需要在所有模块之间共享的提供程序. F/E我有一项服务,可以最终连接到我们的后端系统.不同模块的网址等在单独的提供程序上设置.目前,所有这些服务也是单独的git项目的一部分,因为我也在iOnic应用程序和其他前端中使用了它们.

I'm starting to create separate modules and move away from having everything in the main Module because of obvious reasons. I however, need to have Providers which are shared across all modules. F/E I have a service which does the final connection to our Back end System. Url's etc. for different modules are set on separate providers. All these services are currently part of a separate git project as well since I'm also using them in our iOnic apps and other front-ends.

那么我将如何去拥有该服务,该服务将在我为项目创建的所有模块中共享其实例?

So how would I go and have this service which shares it's instance across all modules I'd create for my project?

我还要创建一个仪表板,以显示当前已安装/可用模块中的小配件".这还取决于用户可以清楚看到的内容. Java中的模拟构造将通过组件扫描/反射解决.在Angular中,我假设我要创建一个全球可用的提供程序,在其中可以添加要放置的Type,然后可以在仪表板上使用它?

Also I'd like to create a Dashboard which shows "widgets" from the currently installed/available modules. This also depends on what users can see obviously. Simular constructions in Java would be resolved by Component Scanning / Reflection. In Angular I'd assume I'd make a global available provider where I could add Type's to be placed which would then be available on the dashboard?

由于模块是分开的,所以最后一部分对于模块可能比较棘手,任何人都可以分享一些想法吗?他们基本上是两个问题,但我相信他们应该以非常类似的答案来解决.

That last part might be tricky with Modules since everything is separated, can anyone share a few thoughts? They're basically 2 questions but I believe they should resolve in a very simular answer.

因此,在我的情况下,我想知道现有的路线,这与我陈述的另一个问题有关,但与之相关.

So in my case I would like to be aware of the existing Routes, this is related to a different question I stated but hooks into this.

我想做的就是这样

@NgModule({
  import:[
   BaseModule,
   someConstContainingRoutes
  ],

}) 
export class SubModuleWithRouting implements OnInit {
   //Import routeAuthService from BasicModule
   constructor(private routeAuthService: RouteAuthService){};

   ngOnInit() {
      this.routeAuthService.addRoutes(someConstContainingRoutes);
   }
}

诸如此类的模式使我不必在主组件或w/e内部的服务中设置路由.当然,我们可以在首次加载或带w/e时执行此操作,但感觉不正确.

A pattern such as this keeps me from messing with setting up the routes in my service inside the main Component or w/e. Of course we could do this on first load or w/e but it wouldn't feel right.

我猜想其他开发者也必须做类似的事情,但我现在真的看不到这里适合哪种模式.

I guess a similar thing must be done by other devs but I can't really see now what kind of pattern would fit here.

也许已经有这样的挂钩了. 我的BaseModule对此模块一无所知.我的路线可选地包含某些许可权值(只是一个枚举).在canActivate钩子上,我想检查一下,因此我觉得它应该知道该路由,因为我似乎无法在canActivate钩子中获取它,并且为每个权限设置它似乎很愚蠢.

Maybe there already is such a hook available. My BaseModule will not know anything about this module. My routes optionally contain certain permission values (which are just an enum). On the canActivate hook I want to check this, so I feel like it should know about the route since I can't seem to fetch it in the canActivate hook and setting it up for every permission seems silly.

推荐答案

模块的提供者是全局提供的.

Providers of modules are provided globally.

一个例外是延迟加载的模块,它们具有自己的子注入器. 延迟加载模块的提供者仅限于此模块.

An exception are lazy loaded modules which get their own child injector. Providers of lazy loaded modules are scoped to this module.

如果要在全局范围内使用延迟加载的模块的提供程序,请使用

If you want providers of lazy loaded modules added globally use

static forRoot()到模块和

export class SharedModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: SharedModule,
      providers: [ UserService ]
    };
  }
}

因此可以将其导入

 imports: [
    SharedModule.forRoot()
  ],

另请参见 https://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared-module-for-root

这篇关于Angular2 RC5跨模块提供程序/扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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