Reputation: 5343
I am having a bit of trouble understanding the logic here
root-component
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 {
}
top-navigation component
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>
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?
Upvotes: 1
Views: 239
Reputation: 31777
As @Bhavik said ROUTER_DIRECTIVES
is required everytime you use RouterLink
or RouterOutlet
(you can specify each one of them as well).
Check the source code for Router
export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);
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})
]);
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.
Upvotes: 4