我知道我可以为routerLink传递一个参数,例如
/user/:id
通过编写
[routerLink]="['/user', user.id]"
但是像这样的路线呢:
/user/:id/details
有没有办法设置这个参数,或者我应该考虑一个不同的URL方案?
我知道我可以为routerLink传递一个参数,例如
/user/:id
通过编写
[routerLink]="['/user', user.id]"
但是像这样的路线呢:
/user/:id/details
有没有办法设置这个参数,或者我应该考虑一个不同的URL方案?
当前回答
app-routing.module.ts
const routes: Routes = [
{ path: 'products', component: ProductsComponent },
{ path: 'product/:id', component: ProductDetailsComponent },
{ path: '', redirectTo: '/products', pathMatch: 'full' },
];
在控制器中,你可以这样导航,
this.router.navigate(['/products', productId]);
它将使您进入如下路径:http://localhost:4200/products/product-id
Ref
其他回答
非常简单
<a routerLink="/user/{{id}}/details"></a>
您可以使用动态值来生成链接。对于动态链接,传递一个路径段数组,后面跟着每个段的参数。 例如,['/team', teamId, 'user', userName, {details: true}]生成一个指向/team/11/user/bob的链接;details=true。
在您的特定示例中,您将执行以下routerLink:
[routerLink]="['user', user.id, 'details']"
要在控制器中做到这一点,你可以注入Router并使用:
router.navigate(['user', user.id, 'details']);
更多信息请参阅Angular文档中的路由和导航中的链接参数数组
也许这个回答有点晚,但是如果你想用param浏览另一个页面,
[routerLink]="['/user', user.id, 'details']"
此外,你不应该忘记路由配置,
[path: 'user/:id/details', component:userComponent, pathMatch: 'full']
constructor(private activatedRoute: ActivatedRoute) {}
在你要用的箱子里
`this.activatedRoute.parmas.subscribe(params => { console.log(params['type']) }); }`
用params代替queryparamas从url -中获取参数