Answers:
在您的特定示例中,您将执行以下操作routerLink
:
[routerLink]="['user', user.id, 'details']"
为此,可以在控制器中注入Router
和使用:
router.navigate(['user', user.id, 'details']);
<button mat-button routerLink="...">View user</button>
语法?
首先在表中配置:
const routes: Routes = [
{
path: 'class/:id/enrollment/:guid',
component: ClassEnrollmentComponent
}
];
现在输入脚本代码:
this.router.navigate([`class/${classId}/enrollment/${4545455}`]);
在另一个组件中接收参数
this.activatedRoute.params.subscribe(params => {
let id = params['id'];
let guid = params['guid'];
console.log(`${id},${guid}`);
});
有多种方法可以实现这一目标。
routerLink属性要求您将routingModule导入功能模块,以防您延迟加载功能模块;或者如果未自动将其添加到AppModule导入数组中,则仅导入app-routing-module。
<a [routerLink]="['/user', user.id]">John Doe</a>
<a routerLink="urlString">John Doe</a> // urlString is computed in your component
// Inject Router into your component
// Inject ActivatedRoute into your component. This will allow the route to be done related to the current url
this._router.navigate(['user',user.id], {relativeTo: this._activatedRoute})
this._router.navigateByUrl(urlString).then((bool) => {}).catch()