簡體   English   中英

Angular2兩個路由到一個組件?

[英]Angular2 two routes to one component?

在Angular 2中,我想定義兩個使用相同Component的路由,但是一個路由是使用特殊預設參數的特殊情況。 現在我想做的是他的:

@RouteConfig([
  { path: '/',  name: 'Home', component: HomeComponent    },
  { path: '/user/:id', name: 'User', component: UserComponent  },
  { path: '/special-user', name: 'User', component: UserComponent, params:{'userId':'123'}  },
])

我需要的是在“/ special-user”的最后一個路由定義中的“params”。 這有可能嗎?

編輯:在路徑/ user /:id中,id是url的一部分,但在特殊用戶的情況下,我希望id不可見,不要成為url的一部分

Angular 2 Beta.x

將路線定義為

@RouteConfig([ 
  ...
  { path: '/special-user/:userId', name: 'SpecUser', component: UserComponent}
])

采用

里面的模板

[routerLink]="['SpecUser', {userId:123}]"

內部組件

export class SomeComponent  {
  constructor(private router: Router) {}

  someMethod() {
    this.router.navigate(['SpecUser', {userId:123}]);
  }
}

Angular 2 RC

將路線定義為

@Routes([ 
  ...
  { path: '/special-user/:userId', component: UserComponent}
])

采用

里面的模板

[routerLink]="['/special-user', 123]"

內部組件

export class SomeComponent  {
  constructor(private router: Router) {}

  someMethod() {
    this.router.navigate(['/special-user', 123]);
  }
}

我在本文中找到了答案:

https://auth0.com/blog/2016/01/25/angular-2-series-part-4-component-router-in-depth/ 1

配置路由

@RouteConfig([
  { path: '/myroute', component: MyComponent, name: 'MyRoute', data: { isAdmin: true } }
])

組件內部:

import {RouteData} from 'angular2/router';

export class MyComponent {
  isAdmin: boolean;

  constructor(data: RouteData) {
    this.isAdmin = data.get('isAdmin');
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM