Search code examples
angularangular2-router3

Register router directives globally with angular router rc3


I do not want to register the Router_Directives for every component.

I want to do it globally as I did before:

import { RouterConfig ,Router, ActivatedRoute, ROUTE_PROVIDERS} from '@angular/router';
bootstrap(AppComponent, [APP_ROUTER_PROVIDERS, HTTP_PROVIDERS, ROUTE_PROVIDERS ]);

but ROUTE_PROVIDERS does not exist/exported in router module.

How can I do that with the RC3 router?


Solution

  • What we need is a multi-provider

    Here's what I did for some pipes that I created that I wanted to make globally available:

    import {provide, PLATFORM_PIPES} from '@angular/core';
    ...
    bootstrap(MegaDashboardAppComponent, [
      HTTP_PROVIDERS,
      APP_ROUTER_PROVIDERS,
      provide(PLATFORM_PIPES,
        {
          useValue: [
            Truthy,
            PrettifiedCamel,
            KeysPipe,
            IfDate
          ],
          multi: true
        }) // provide pipes globally
    ]);
    

    I imagine you can do the same for any directives that you need globally available as well implemented as such:

    import {provide, PLATFORM_PIPES, PLATFORM_DIRECTIVES} from '@angular/core';
    import {ROUTER_DIRECTIVES} from '@angular/router';
    ...
    bootstrap(Example, [
      APP_ROUTER_PROVIDERS,
      ...
      provide(PLATFORM_DIRECTIVES,
        {
          useValue: [ROUTER_DIRECTIVES],
          multi: true
        }) 
    ]);
    

    Just tested, Works wonderfully, especially with Angular material 2 directives as well :)