0

我不想为每个组件注册 Router_Directives。

我想像以前一样在全球范围内这样做:

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

但路由器模块中不存在/导出 ROUTE_PROVIDERS。

如何使用 RC3 路由器做到这一点?

4

1 回答 1

1

我们需要的是一个多供应商

以下是我为我创建的一些管道所做的事情,这些管道我想在全球范围内使用:

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
]);

我想您可以对需要全局可用的任何指令执行相同的操作,并且可以这样实现:

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
    }) 
]);

刚刚测试过,效果很好,尤其是 Angular 材料 2 指令:)

于 2016-06-29T19:16:42.083 回答