https://angular.io/api/router/RouterLink很好地概述了如何创建链接,将用户带到Angular4中的不同路由,但是我找不到如何以编程方式执行相同的操作,而需要用户单击链接
https://angular.io/api/router/RouterLink很好地概述了如何创建链接,将用户带到Angular4中的不同路由,但是我找不到如何以编程方式执行相同的操作,而需要用户单击链接
routerLink
指令的用法如下:
<a [routerLink]="/inbox/33/messages/44">Open Message 44</a>
只是使用router
及其导航球方法对命令式导航进行包装:
router.导航球('/inbox/33/messages/44')
从消息来源可以看出:
export class RouterLink {
...
@HostListener('click')
onClick(): boolean {
...
this.router.导航球(this.urlTree, extras);
return true;
}
因此,无论您需要将用户导航到何处,只需输入router
并使用导航球
方法:
class MyComponent {
constructor(router: Router) {
this.router.导航球(...);
}
}
路由上还有另一种方法可以使用-导航:
router.导航(['/inbox/33/messages/44'])
使用
router.导航球
类似于更改位置栏为了清楚地看到差异,假设当前的URL是
使用此URL,呼叫
在the official docs分钟内阅读更多内容.