我有两条路线,其中path
和name
不同,但component
相同。有没有办法检查我导航前的路径是什么。到/add-user
或name
为前。 AddUser
app.component.ts
@RouteConfig([
{
path: '/add-user',
name: 'AddUser',
component: UserComponent
},
{
path: '/user/:id/detail',
name: 'UserDetail',
component: UserComponent
}
])
user.component.ts
ngOnInit() {
//this is pseudocode
if (this._router.name == 'AddUser') {
console.log('you want to add a user')
}
else{
console.log('you want to see the details of a user');
}
答案 0 :(得分:2)
您要找的是:router.currentInstruction.component.routeName
小免责声明:Angular 2.0 RC中引入的新新路由器不支持路由名称。您可能希望避免使用它。
答案 1 :(得分:2)
> = Angular2 RC.0< = RC.3
新路由器中不再有别名。
您可以获取本地组件的相对路径
routerOnActivate(curr:RouteSegment, prev?:RouteSegment, currTree?:RouteTree, prevTree?:RouteTree):void {
console.log(curr.stringifiedUrlSegments);
}
或使用
的完整路径constructor(private router:Router) {}
routerOnActivate(curr:RouteSegment, prev?:RouteSegment, currTree?:RouteTree, prevTree?:RouteTree):void {
console.log(this.router.serializeUrl(tree));
}
或
constructor(router:Router, private location:Location) {
router.changes.subscribe(() => {
console.log(this.location.path());
});
}