加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在Angular中使用router.navigateByUrl和router.navigate

发布时间:2020-12-17 07:57:55 所属栏目:安全 来源:网络整理
导读:https://angular.io/api/router/RouterLink很好地概述了如何在Angular4中创建将用户带到不同路径的链接,但是我找不到如何以编程方式执行相同的操作而不需要用户单击链接 navigateByUrl 如下所用的routerLink指令: a [routerLink]="/inbox/33/messages/44"Op
https://angular.io/api/router/RouterLink很好地概述了如何在Angular4中创建将用户带到不同路径的链接,但是我找不到如何以编程方式执行相同的操作而不需要用户单击链接
navigateByUrl

如下所用的routerLink指令:

<a [routerLink]="/inbox/33/messages/44">Open Message 44</a>

只是使用路由器及其navigateByUrl方法的命令式导航的包装:

router.navigateByUrl('/inbox/33/messages/44')

从资料来源可以看出:

export class RouterLink {
  ...

  @HostListener('click')
  onClick(): boolean {
    ...
    this.router.navigateByUrl(this.urlTree,extras);
    return true;
  }

因此,只要您需要将用户导航到另一个路由,只需注入路由器并使用navigateByUrl方法:

class MyComponent {
   constructor(router: Router) {
      this.router.navigateByUrl(...);
   }
}

导航

您可以使用路由器上的另一种方法 – navigate:

router.navigate(['/inbox/33/messages/44'])

两者之间的差异

Using router.navigateByUrl is similar to changing the location bar
directly–we are providing the “whole” new URL. Whereas
router.navigate creates a new URL by applying an array of passed-in
commands,a patch,to the current URL.

To see the difference clearly,imagine that the current URL is
'/inbox/11/messages/22(popup:compose)'.

With this URL,calling
router.navigateByUrl('/inbox/33/messages/44') will result in
'/inbox/33/messages/44',and calling
router.navigate('/inbox/33/messages/44') will result in
'/inbox/33/messages/44(popup:compose)'.

阅读更多the official docs.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读