Angular2:如何直接从根视图链接到(大)子视图

时间:2016-04-07 09:50:45

标签: angularjs angular angular2-routing

我正在尝试Angular 2并遇到了一个我不确定如何调试的问题。

示例情况

我有AppComponent处理所有基本级别路由,并且每个“级别”路由下来都在该级别的新路由器上声明。

我有以下路线:

  • / home,在AppComponent
  • 上定义
  • / applications,在AppComponent
  • 上定义
  • / applications / new,在ApplicationRouterComponent
  • 上定义
  • / applications / foo,在ApplicationRouterComponent
  • 上定义

作为周围模板的一部分,有一个导航栏,对所有页面都是通用的,因此需要能够链接到子路由器中定义的任何路由。 [routerLink]嵌套组件不会导致任何错误抛出。

问题

当链接到孙子路由时,看起来View组件甚至不构造。即,

<a [routerLink]="['./ApplicationRouter/New']">New Application</a>

构建并显示ApplicationRouterComponent,但NewApplication组件不会。

代码示例

appComponent.ts

import {Component} from 'angular2/core';
import {
  RouteConfig,
  ROUTER_DIRECTIVES,
  ROUTER_PROVIDERS
} from 'angular2/router';
import {ApplicationRouterComponent} from './routes/application/applicationRouter';
import {HomeComponent} from './routes/home/homeComponent';

@Component({
  selector: 'bop-application',
  templateUrl: 'app/index.html',
  directives: [ROUTER_DIRECTIVES],
  providers: [ROUTER_PROVIDERS],
})
@RouteConfig([
    { path: '/home', name: 'Home', component: HomeComponent, useAsDefault: true },
    { path: '/applications/...', name: 'ApplicationRouter', component: ApplicationRouterComponent }
])
export class AppComponent { }

./应用程序/路由/应用/ applicationRouter

import {Component} from 'angular2/core';
import {
  RouteConfig,
  ROUTER_DIRECTIVES,
  ROUTER_PROVIDERS
} from 'angular2/router';
import {NewApplicationComponent} from './new/newApplicationComponent';
import {FooComponent} from './new/foo';

@Component({
  selector: 'application-router',
  templateUrl: 'app/routes/application/index.html',
  directives: [ROUTER_DIRECTIVES],
  providers: [ROUTER_PROVIDERS],
})
@RouteConfig([
  { path: '/new', name: 'New', component: NewApplicationComponent},
  { path: '/foo', name: 'Foo', component: FooComponent},
])
export class ApplicationRouter {
  constructor() {
    debugger;
  }
}

./应用/ index.html中

<h1>Portal</h1>
<nav>
    <a [routerLink]="['Home']">Home</a>
    <a [routerLink]="['./ApplicationRouter/New']">New Application</a>
</nav>
<router-outlet></router-outlet>

./应用程序/应用/ index.html中

Application Router
<router-outlet></router-outlet>

./应用程序/应用/新/ new.html

<h4>New View</h4>

1 个答案:

答案 0 :(得分:5)

路由器链接应为

<a [routerLink]="['/ApplicationRouter', 'New']">New Application</a>

路由器链接获取路由名称列表,而不是路径。支持的某些字符在路径中被解释

  • /xxx从root router
  • 开始
  • ..从父路由器
  • 开始
  • ./xxx用于当前路由器(AFAIK冗余)