如何从父组件和子路由获取 RouteParams
export const routes: Routes = [
{ path: '', redirectTo: 'list', pathMatch: 'full' },
{ path: 'list', component: UsersComponent },
{
path: ':id', component: UserDetailComponent,
children: [
// { path: '', redirectTo: 'overview', pathMatch: 'full' },
{ path: 'overview', component: UserInfoComponent },
{ path: 'specs/:id', component: UserProfileComponent }
]
}
];
在组件中
export class UserDetailComponent implements OnInit {
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.parent.params.subscribe( (c) => {
console.log(c['id']);
});
this.route.params.subscribe(params => {
console.log(params['id']);
});
}
}
但是当我的路线被激活时,我总是变得不确定。 请帮助解决这个问题?
提前致谢。
最佳答案
这是一个如何在.ts中获取参数值的简单示例, 之后,您就可以根据自己的情况对其进行自定义
import { ActivatedRoute } from '@angular/router';
constructor(private route: ActivatedRoute) {
const taskId: string = route.snapshot.params["taskId"];
console.log("this is taskId value = "+ taskId);
}
关于Angular2获取激活的路由参数和父参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41565056/