出于相同目的在两个地方使用路由器指令
Router directives in two places for same purpose
我有点难以理解这里的逻辑
根组件
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
<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>
我的问题是,虽然这行得通,但您能解释一下为什么我需要将 Router 指令放在两个地方吗?是否有更好的方法来做到这一点?
正如@Bhavik 所说,每次使用 RouterLink
或 RouterOutlet
时都需要 ROUTER_DIRECTIVES
(您也可以指定它们中的每一个)。
检查 Router
的源代码
export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);
显然,每次使用其中任何一个时都添加它很烦人,因此您可以使用 PLATFORM_DIRECTIVES 使其更简单。这样,您只需将它添加到您的应用程序中,它就可以在整个应用程序中使用。
bootstrap(App, [
provide(PLATFORM_DIRECTIVES, {useValue: [ROUTER_DIRECTIVES], multi: true})
]);
请注意,有一个 issue 公开建议将 ROUTER_DIRECTIVES
添加到 ROUTER_PROVIDERS
,因此我们甚至可以跳过上面建议的解决方案。这将使设置路由器更容易。
我有点难以理解这里的逻辑
根组件
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
<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>
我的问题是,虽然这行得通,但您能解释一下为什么我需要将 Router 指令放在两个地方吗?是否有更好的方法来做到这一点?
正如@Bhavik 所说,每次使用 RouterLink
或 RouterOutlet
时都需要 ROUTER_DIRECTIVES
(您也可以指定它们中的每一个)。
检查 Router
的源代码export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);
显然,每次使用其中任何一个时都添加它很烦人,因此您可以使用 PLATFORM_DIRECTIVES 使其更简单。这样,您只需将它添加到您的应用程序中,它就可以在整个应用程序中使用。
bootstrap(App, [
provide(PLATFORM_DIRECTIVES, {useValue: [ROUTER_DIRECTIVES], multi: true})
]);
请注意,有一个 issue 公开建议将 ROUTER_DIRECTIVES
添加到 ROUTER_PROVIDERS
,因此我们甚至可以跳过上面建议的解决方案。这将使设置路由器更容易。