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

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

问题描述

我在理解这里的逻辑时遇到了一些麻烦

根组件

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";从angular2/router"导入{RouteConfig};从 "angular2/router" 导入 { ROUTER_DIRECIVES };@成分({选择器:ng2-app",模板:`<section class="jumbotron full-height"><top-navigation></top-navigation><div class="容器"><路由器插座></路由器插座>

</节>`,指令:[TopNavigationComponent、ArcListComponent、ROUTER_DIRECTICIVES]})@RouteConfig([{path: "/", name: "Root", component: ArcListComponent, useAsDefault: true},{path: "/new", name: "New-item", component: ArcNewItemComponent}])导出类 RootComponent {}

顶部导航组件

import { Component } from "angular2/core";从 "angular2/router" 导入 { ROUTER_DIRECIVES };@成分({选择器:顶部导航",templateUrl: "dev/shared/navigation.template.html",指令:[ROUTER_DIRECIVES]})导出类 TopNavigationComponent {}

导航.模板

<!-- 收集导航链接、表单和其他用于切换的内容--><div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"><ul class="nav navbar-nav navbar-right"><li><a [routerLink]="['Root']">首页</a></li><li><a [routerLink]="['New-item']">添加新资源</a></li><li><a href="#">Github</a></li></div><!--/.navbar-collapse --></div><!--/.container-fluid --></nav>

我的问题是,虽然这可行,您能解释一下为什么我需要将路由器指令放在两个地方吗,以及是否有更好的方法来做到这一点?

解决方案

正如@Bhavik 所说,每次使用 RouterLinkRouterOutlet 时都需要 ROUTER_DIRECTIVES>(您也可以指定其中的每一个).

检查路由器的源代码

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

显然,每次使用其中任何一个时都添加它很烦人,因此您可以使用 PLATFORM_DIRECTIVES.这样,您只需在应用程序中添加一次,即可在整个应用程序中使用它.

bootstrap(App, [提供(PLATFORM_DIRECTIVES,{useValue:[ROUTER_DIRECTIVES],多:真})]);

请注意,有一个 issue 公开提议添加 ROUTER_DIRECTIVESROUTER_PROVIDERS,所以我们甚至可以跳过上面建议的解决方案.这将使设置路由器变得更加容易.

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?

解决方案

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.

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

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆