Angular2获取激活的路由参数和父参数
发布时间:2020-12-17 06:48:57 所属栏目:安全 来源:网络整理
导读:如何从父组件以及子路由获取RouteParams export const routes: Routes = [ { path: '',redirectTo: 'list',pathMatch: 'full' },{ path: 'list',component: UsersComponent },{ path: ':id',component: UserDetailComponent,children: [ // { path: '',redir
如何从父组件以及子路由获取RouteParams
export const routes: Routes = [ { path: '',redirectTo: 'list',pathMatch: 'full' },{ path: 'list',component: UsersComponent },{ path: ':id',component: UserDetailComponent,children: [ // { path: '',redirectTo: 'overview',{ 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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |