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

如何测试Angular2中是否存在路由?

发布时间:2020-12-17 17:46:52 所属栏目:安全 来源:网络整理
导读:在Angular2中,如何检查路由是否存在? 我有一个方法,可以记住如果用户未经授权,导航到哪个路由. 登录时我有: this.router.navigate([this.authService.redirectUrl]); 但我只想导航如果redirectUrl引用了一个有效的路由,比如.. if (this.authService.redire
在Angular2中,如何检查路由是否存在?

我有一个方法,可以记住如果用户未经授权,导航到哪个路由.

登录时我有:

this.router.navigate([this.authService.redirectUrl]);

但我只想导航如果redirectUrl引用了一个有效的路由,比如..

if (this.authService.redirectUrl is a valid route) {
  this.router.navigate([this.authService.redirectUrl]);
}

有没有办法检查这个?

解决方法

您可以使用promise方式检查路由是否存在:

this.router.navigate(['redirect'])
  .then(data => {
    console.log('Route exists,redirection is done');
  })
  .catch(e => {
    console.log('Route not found,redirection stopped with no error raised');
  });

如果定义了路由,则会发生重定向.如果没有,您可以在catch块中执行一些代码.

(编辑:李大同)

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

    推荐文章
      热点阅读