从url获取参数

bjg7j2ky  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(417)

我想从我的url获取参数。我的路线结构如下

{
    path: 'customers/viewCustomer/:id',component:CommonSingleCustomerPageComponent, canActivate: [AuthGuard], data:{ permission:['Manage-Customer']},
    children: [
      {
        path: 'detail',
        component: ViewCustomerComponent, canActivate: [AuthGuard],data:{permission: ['Manage-Customer']}
      },
}

但当我试图在viewcustomercomponent中获取:id时,我会将其视为未定义。我尝试过以下方法:

export class ViewCustomerComponent implements OnInit {
    constructor(
            private router: ActivatedRoute,
          ) { }

          data: any = '';
          id: any = '';

          ngOnInit(): void {
            this.id = this.router.snapshot.params.id
            console.log(this.id)
    }
    }

而且,

export class ViewCustomerComponent implements OnInit {

  constructor(
    private router: ActivatedRoute,
  ) { }

  data: any = '';
  id: any = '';

  ngOnInit(): void {
    this.id = this.router.snapshot.paramMap.get('id')
    console.log(this.id)
  }
}

但这些都不管用。你知道我做错了什么吗?

wvt8vs2t

wvt8vs2t1#

您希望访问在父级中定义的路径路由。因此,使用父对象访问子组件viewcustomercomponent中的值(:id)

this.router.parent.params

或者参考本文https://ultimatecourses.com/blog/angular-parent-routing-params

相关问题