Angular2 RC5 Cross-Module Provider / Scanning

浪尽此生 提交于 2019-12-24 00:23:41

问题


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

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?

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.

Edit:

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.

What I would like to do is something like this

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

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

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

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.

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.


回答1:


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() to your module and

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

so it can be imported with

 imports: [
    SharedModule.forRoot()
  ],

See also https://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared-module-for-root



来源:https://stackoverflow.com/questions/39036489/angular2-rc5-cross-module-provider-scanning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!