如何将多个参数传递给 Angular 路由器?
How to pass multi parameters to Angular router?
我知道我们可以这样做:
{ path:'/entity/:id/:action', component: EntityComponent }
但是,我想为路径做一些事情:
{ path: '/entity/:id/sub-entity/:id2', component SubEntityComponent }
知道怎么做吗?
谢谢。
--------更新了------
如何同时进行:
{ path: 'entity/:id/sub-entity/:id2', component: SubEntityComponent }
和
{ path: 'entity/:id/sub-obj/:id2, component: SubObjComponent' }
??
谢谢
在文件中routes.ts
{ path: 'myUrlpath/:id1/:id2', component: componentToGoTo},
调用路由器
this.router.navigate(['myUrlPath/'+"someId"+"/"+"anotherID"]);
你的第二个路径中有一个带引号的小拼写错误:
{ path: 'entity/:id/sub-obj/:id2', component: SubObjComponent }
您可以像这样绑定 RouterLink:
<a [routerLink]="['/entity', id, 'sub-entity', id2]">SubEntityComponent</a>
<a [routerLink]="['/entity', id, 'sub-obj', id2]">SubObjComponent</a>
检查演示 HERE。
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'entity/:id/sub-entity/:id2',
component: SubEntityComponent
},
{
path: '/entity/:id/sub-entity/:id2',
component SubEntityComponent
}
];
// in component:
// '/entity/:id/:action'
const id; // your param
const action; // your action
this.router.navigate(['entity', id, action]);
// 'entity/:id/sub-entity/:id2'
const id; // your first param
const id2; // your second param
this.router.navigate(['entity', id, 'sub-entity', id2]);
我知道我们可以这样做:
{ path:'/entity/:id/:action', component: EntityComponent }
但是,我想为路径做一些事情:
{ path: '/entity/:id/sub-entity/:id2', component SubEntityComponent }
知道怎么做吗?
谢谢。
--------更新了------
如何同时进行:
{ path: 'entity/:id/sub-entity/:id2', component: SubEntityComponent }
和
{ path: 'entity/:id/sub-obj/:id2, component: SubObjComponent' }
??
谢谢
在文件中routes.ts
{ path: 'myUrlpath/:id1/:id2', component: componentToGoTo},
调用路由器
this.router.navigate(['myUrlPath/'+"someId"+"/"+"anotherID"]);
你的第二个路径中有一个带引号的小拼写错误:
{ path: 'entity/:id/sub-obj/:id2', component: SubObjComponent }
您可以像这样绑定 RouterLink:
<a [routerLink]="['/entity', id, 'sub-entity', id2]">SubEntityComponent</a>
<a [routerLink]="['/entity', id, 'sub-obj', id2]">SubObjComponent</a>
检查演示 HERE。
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'entity/:id/sub-entity/:id2',
component: SubEntityComponent
},
{
path: '/entity/:id/sub-entity/:id2',
component SubEntityComponent
}
];
// in component:
// '/entity/:id/:action'
const id; // your param
const action; // your action
this.router.navigate(['entity', id, action]);
// 'entity/:id/sub-entity/:id2'
const id; // your first param
const id2; // your second param
this.router.navigate(['entity', id, 'sub-entity', id2]);