Angular 使用多个子对象时如何使用routerLink?

Angular 使用多个子对象时如何使用routerLink?,angular,angular2-routing,Angular,Angular2 Routing,这是AdminRouting path: 'adminHome', component: adminHomeComponent, children: [ { path: '', component: HomeComponent, }, { path: 'home', component: HomeComponent,

这是AdminRouting

    path: 'adminHome',
    component: adminHomeComponent,
    children: [
        {
            path: '',
            component: HomeComponent,

        },
        {
            path: 'home',
            component: HomeComponent,

        },
        {
            path: 'users',
            component: UserListComponent,
            children: [
                {
                    path: '',
                    component: HUComponent
                },
                {
                    path: ':id',
                    component: EntrepriseListComponent,
                    children: [
                        {
                            path: '',
                            component: HyComponent

                        },
                        {
                            path: ':id',
                            component: ListLaucauxComponent
                        },
                    ]
                }

            ]
        },
    ]
  },
我想通过单击企业的id来获取List LocauxComponent,以便获取这个URL localhost:3000/adminHome/users/{{id_user}}/{id_enterprise} 我尝试使用
[routerLink]=“['/adminHome/users/:id',x.id]”
但我得到了另一个类似的URL
您的链接错误。当使用
[routerLink]
时,您不需要指定
:id
,就像您不需要使用path变量那样,它被定义为放置id的占位符

[routerLink]
应该如下所示:

[routerLink]="['/adminHome/users', x.id]"
[routerLink]="['/adminHome/users', param1, param2...]"
编辑

要使用更多参数构建
[routerLink]
,请执行以下操作:

[routerLink]="['/adminHome/users', x.id]"
[routerLink]="['/adminHome/users', param1, param2...]"

这将导致:
/adminHome/users/1/2
如果
param1=1
param2=2

此URL中有什么错误?我在后端服务器上收到一个nullPointerException,您可以通过在浏览器上手动输入来检查您是否使用正确的URL获取数据吗?是的,它可以工作,并且在浏览器上显示我想要获取的内容,但在控制台存在以下错误:GET 500()&&EXCEPTION:Observable_1.Observable.throw不是函数&ORIGINAL STACKTRACE:这也是:TypeError:Observable_1.Observable.throw不是CatchSubscriber.enterpriseseservice.handleError[作为选择器](enterprise.service.ts:44)在CatchSubscriber.error(catch.ts:55)上的函数在MapSubscriber.Subscriber.error(Subscriber.ts:139)在MapSubscriber.Subscriber.error(Subscriber.ts:109)在XMLHttpRequest.onLoad(http.umd.js:1185)在ZoneDelegate.invokeTask(zone.js:367)在Object.onInvokeTask(core.umd.js:3971)在ZoneDelegate.invokeTask(zone.js:6)在zone.runTask(zone.js:166)在MapSubscriber在XMLHttpRequest.ZoneTask.invoke(zone.js:but[routerLink]=“['/adminHome/users',x.id]”将调用EnterpriseListComponent,如您在routing.ts上所看到的。问题是,它的工作方式与我所希望的一样,并且由于url上的id更改为%3Aid,因此服务器上存在空指针异常。当我手动更改%3Aid的正确id时,没有空指针异常,我无法找到解决方案,如何保持url像这样ocalhost:3000/adminHome/users/{{id\u user}}/{{id\u enterprise}}所以它应该是
[routerLink]=“['/adminHome/users',x.id,param2]”
在构建链接时,不能放置
/:id
,因为它将以纯文本的形式发送(
%3A
的URL编码版本:
).routerLink将自动为您构建URL,如果您操作正确,这意味着将基本URL作为第一个参数,作为字符串,根据需要与每个参数相连,如下所示:
[routerLink]=“['/adminHome/users',param1,param2,param3…”
它将是[routerLink]=“['/adminHome/users',param2,x.id]”。我应该在params2中放入什么,以便用{id_user}替换parms2,以及如何重复执行{id_user}并将其替换为[routerLink]。我尝试在ngOnInit()中获取此id,并将其替换为“['/adminHome/users',{id},x.id]”但是这种语法是不允许的。您需要使用component.ts文件中的参数。如果参数的名称是
id
,那么在
[routerLink]
中注入参数时,您不需要
{}
。它与任何其他角度指令一样工作。因此它可能类似:
“['/adminHome/users',id,x.id]”
如果
id
x.id
被配置为组件中的参数。