在两个地方为同一目的路由器指令 [英] Router directives in two places for same purpose

查看:178
本文介绍了在两个地方为同一目的路由器指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点麻烦在这里认识逻辑

I am having a bit of trouble understanding the logic here

根组件

import { Component } from "angular2/core";
import { TopNavigationComponent } from "./shared/navigation.component";
import { ArcListComponent } from "./arc/arc-list.component";
import { ArcNewItemComponent } from "./arc/arc-new-item.component";
import { RouteConfig } from "angular2/router";
import { ROUTER_DIRECTIVES } from "angular2/router";
@Component({
  selector: "ng2-app",
  template: `
    <section class="jumbotron full-height">
      <top-navigation></top-navigation>
      <div class="container">
        <router-outlet></router-outlet>
      </div>
    </section>
  `,
  directives: [TopNavigationComponent, ArcListComponent,ROUTER_DIRECTIVES]
})

@RouteConfig([
  {path: "/", name: "Root", component: ArcListComponent, useAsDefault: true},
  {path: "/new", name: "New-item", component: ArcNewItemComponent}
])

export class RootComponent {

}

顶级导航组件

import { Component } from "angular2/core";
import { ROUTER_DIRECTIVES } from "angular2/router";

@Component({
    selector: "top-navigation",
    templateUrl: "dev/shared/navigation.template.html",
    directives: [ROUTER_DIRECTIVES]
})

export class TopNavigationComponent {

}

navigation.template

navigation.template

<nav class="navbar navbar-default">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" [routerLink]="['Root']">Angular2Arc</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
        <li><a [routerLink]="['Root']">Home</a></li>
        <li><a [routerLink]="['New-item']">Add New Resource</a></li>
        <li><a href="#">Github</a></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

我的问题是,而这个工程,你能解释一下为什么我需要把路由器的指令在两个地方,如果有更好的方法来做到这一点?

my question is while this works, can you explain why i need to put Router directives in two places, and if there is a better way to do this?

推荐答案

由于@Bhavik说 ROUTER_DIRECTIVES 您使用需要每次 RouterLink RouterOutlet (可以指定他们每个人也一样)。

As @Bhavik said ROUTER_DIRECTIVES is required everytime you use RouterLink or RouterOutlet (you can specify each one of them as well).

检查源$ C ​​$ C为路由器

Check the source code for Router

export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);

显然,添加它每次您使用的是其中一人很讨厌,所以你可以把它简单使用的 PLATFORM_DIRECTIVES 。这样,你会在你的应用程序一次添加它,它会跨越它可用。

Obviously adding it everytime you're using either of one of them is annoying, so you can make it simpler by using PLATFORM_DIRECTIVES. This way you'll add it once in your application and it will be available across of it.

bootstrap(App, [
    provide(PLATFORM_DIRECTIVES, {useValue: [ROUTER_DIRECTIVES], multi: true})
]);

请注意,有一个问题打开提议增加 ROUTER_DIRECTIVES ROUTER_PROVIDERS ,所以我们甚至可以跳过上述解决方案建议。这将使设置路由器要容易得多。

Note that there's an issue open proposing to add ROUTER_DIRECTIVES to ROUTER_PROVIDERS, so we can even skip the solution suggested above. That would make setting up Router much easier.

这篇关于在两个地方为同一目的路由器指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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