首页 文章

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

提问于
浏览
3

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

Example situation

我有一个处理所有基本级别路由的 AppComponent ,并且每个路由的'level'都在该级别的新路由器上声明 .

我有以下路线:

  • / home,在 AppComponent 上定义

  • / applications,在 AppComponent 上定义

  • / applications / new,在 ApplicationRouterComponent 上定义

  • / applications / foo,在 ApplicationRouterComponent 上定义

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

Problem

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

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

构造并显示 ApplicationRouterComponent ,但 NewApplication 组件没有 .

Code Sample

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 { }

./app/routes/application/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;
  }
}

./app/index.html

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

./app/application/index.html

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

./app/application/new/new.html

<h4>New View</h4>

1 回答

  • 5

    路由器链接应该是

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

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

    • /xxx 从根路由器开始

    • .. 从父路由器开始

    • ./xxx 用于当前路由器(AFAIK冗余)

相关问题