如何从父组件和子路由获取 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);
}
您需要 Activated RouteSnapshot 来遍历激活的路由。
https://angular.io/docs/ts/latest/api/router/index/ActivatedRouteSnapshot-interface.html
var myParamValue = this.route.snapshot.queryParams['myParam'];