Angular2 RC1子路由已定义但未被识别

前端之家收集整理的这篇文章主要介绍了Angular2 RC1子路由已定义但未被识别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
曾经用于beta / …的工作已经不再适用了.使用新的新RC1路由器,我该如何进行子路由?

文件夹结构

app
  |--home/
  |    |--home.component.ts
  |--login/
  |    |--login.ts
  |--appShell/
  |    |--app.component.ts
  |--apps/
       |--hero/
            |--hero-shell.component.ts  
            |--horees.component.ts 
            |--hero-detail.component.ts

计划的导航是

app.component -> home.component -> heroshell.component -> heroes.component

app.component.ts路线

@Routes([
    { path: '/heroshell',component: HeroShellComponent },{ path: '/home',component: HomeComponent },{ path: '/login',component: Login },])
export class AppComponent implements OnInit {
  title = 'Apps';       
  constructor(public _auth: Authentication,public router: Router,private _dialogService: DialogService
              ) {}      
  ngOnInit() {
    this.router.navigate(['/login']);
  }
  .......

登录成功后,Login类将会

this.router.navigate(['/home']);

从HomeComponent.ts我可以做到

this.router.navigate(['/heroshell']);

到目前为止这么好,没问题.
在hero-shell.component.ts中我有子路由

@Component({
  selector: 'my-app1',templateUrl: 'app/apps/hero/hero-shell.component.html',styleUrls: ['app/appshell/app.component.css'],directives: [ROUTER_DIRECTIVES],providers: [HeroService]
})

@Routes([
    { path: 'heroes',component: HeroesComponent },{ path: 'dashboard',component: DashboardComponent },{ path: 'hero/:_id',component: HeroDetailComponent},])
export class HeroShellComponent { // implements OnInit
  title = 'Hero Sample App';

  constructor(public _auth: Authentication,public router: Router
              ) {} 
}

在hero-shell.component.html中

<my-app1>

<h1>{{title}}</h1>
<!--<button *ngIf="_auth.loggedIn" (click)="onlogout()">logout</button>-->

<nav>
    <a [routerLink]="['dashboard']">Dashboard</a>
    <a [routerLink]="['heroes']">Heroes</a>
</nav>
<router-outlet></router-outlet>

</my-app1>

请注意,路径只能是“英雄”.如果我改为[routerLink]并且@Routes改为’/ heroes’则不行.可以帮助解释一下原因吗?

因此它现在可以识别儿童路线.点击routerLink英雄将显示英雄列表.但是当我从HeroesComponent中详细介绍它时

this._router.navigate(['hero/'+hero._id]);

它爆炸了

browser_adapter.ts:78 EXCEPTION: Error: Uncaught (in promise): 
Cannot match any routes. Current segment: 'hero'. 
Available routes: ['/heroshell','/home','/login'].

这很奇怪.如果它无法识别儿童路线,我怎么能首先进入HeroesComponent?现在儿童路线刚刚消失.

如果我使用

this._router.navigate(['hero/'+hero._id],this.currSegment);

它会给出不同的错误

browser_adapter.ts:78 EXCEPTION: 
Error: Uncaught (in promise): Component 'HeroesComponent' does not have route configuration

那么这是否意味着所有子组件必须重复使用@Routes?

Note that the path can only be ‘heroes’. if I change to [routerLink] and @Routes to ‘/heroes’ it won’t work. Can some help explain why?

实际上子路由器中的路径可以包含“/”作为前缀,但将routerLink更改为“/ heroes”使其无效,因为导航路径中的“/”前缀将使用根路由器解析,并且没有“/ heroes”路径. “英雄”路径有效,因为它将使用当前路由器解析,并且您在当前子路由器中定义了该路径.

this._router.navigate([‘hero/’+hero._id]);

使用根路由器将解析在没有段的情况下调用“导航”.这意味着你想做绝对导航.显然这不起作用.

this._router.navigate([‘hero/’+hero._id],this.currSegment);

这也没有用,因为你在HeroesComponent和“英雄”段,组件中没有路由器配置.正确的电话应该是:

this._router.navigate(['../hero',{_id: hero._id}],this.currSegment);
原文链接:https://www.f2er.com/angularjs/141984.html

猜你在找的Angularjs相关文章