是否可以通过多个路径参数,例如像下面需要通过id1
和id2
到component B
@RouteConfig([
{path: '/component/:id :id2',name: 'MyCompB', component:MyCompB }
])
export class MyCompA {
onClick(){
this._router.navigate( ['MyCompB', {id: "someId", id2: "another ID"}]);
}
}
好意识到了一个错误..它必须是/:id/:id2
无论如何,在任何教程或其他StackOverflow问题中都没有找到它。
@RouteConfig([{path: '/component/:id/:id2',name: 'MyCompB', component:MyCompB}])
export class MyCompA {
onClick(){
this._router.navigate( ['MyCompB', {id: "someId", id2: "another ID"}]);
}
}
正如本answer中详述的那样,mayur和user3869623的答案现在与弃用的路由器有关。您现在可以传递多个参数,如下所示:
要呼叫路由器:
this.router.navigate(['/myUrlPath', "someId", "another ID"]);
在routes.ts中:
{ path: 'myUrlpath/:id1/:id2', component: componentToGoTo},
两种方法在Angular中传递多路径参数
方法1
在app.module.ts中
将路径设置为component2。
imports: [
RouterModule.forRoot(
[ {path: 'component2/:id1/:id2', component: MyComp2}])
]
调用路由器以多个参数id1和id2导航到MyComp2。
export class MyComp1 {
onClick(){
this._router.navigate( ['component2', "id1","id2"]);
}
}
方法2
在app.module.ts中
将路径设置为component2。
imports: [
RouterModule.forRoot(
[ {path: 'component2', component: MyComp2}])
]
调用路由器以多个参数id1和id2导航到MyComp2。
export class MyComp1 {
onClick(){
this._router.navigate( ['component2', {id1: "id1 Value", id2:
"id2 Value"}]);
}
}
new AsyncRoute({path: '/demo/:demoKey1/:demoKey2', loader: () => {
return System.import('app/modules/demo/demo').then(m =>m.demoComponent);
}, name: 'demoPage'}),
export class demoComponent {
onClick(){
this._router.navigate( ['/demoPage', {demoKey1: "123", demoKey2: "234"}]);
}
}