2

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.

Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75

1 Answers1

2

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

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I couldn't find this in the docs: How about providers do they share their instance? Is it the same instance across all modules who import them? I guess there is no way to avoid having to import them everywhere then? – Mathijs Segers Aug 19 '16 at 11:25
  • I'll try this Gunther, question: Is there a way I could do something with a service on Module init? I"d like to add my routes to a service on load, so I can properly use them in canActivate, or even show them in a menu dynamically – Mathijs Segers Aug 22 '16 at 08:04
  • Just assign the routes to a variable (like it's done in the tutorial I linked to in my answer) and import the file containing the variable wherever you need it (in addition to provide them to the router). – Günter Zöchbauer Aug 22 '16 at 08:07
  • So this is probably not possible in the Module? Or could I even do a OnInit on the router class and construct the service instance? That would be cool. Then I could repeat this pattern for all my modules. Otherwise I wouldn't know where to put it honestly. – Mathijs Segers Aug 22 '16 at 08:27
  • Not sure why you think you need a module for that. Perhaps adding the code to your question that demonstrates what you try to accomplish might help to understand the problem. – Günter Zöchbauer Aug 22 '16 at 08:28
  • I hope it's not too jibberish maybe you understand what I truely want. And I kinda feel that something like my wish is already available. – Mathijs Segers Aug 22 '16 at 08:39
  • You can't use `imports: []` of `NgModule` to import routes. This is only to import other `NgModules`. You would need a normal TypeScript import. – Günter Zöchbauer Aug 22 '16 at 08:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121514/discussion-between-mathijs-segers-and-gunter-zochbauer). – Mathijs Segers Aug 22 '16 at 08:56