代码之家  ›  专栏  ›  技术社区  ›  Santhosh

如何知道导航是否被取消?

  •  0
  • Santhosh  · 技术社区  · 2 年前

    我正在导航到一条路线,但其中一个路线守卫在13中返回false。 如何知道导航是完成还是取消。 我已经做了下面的工作,但是块和最终块控制台正在打印,即使route guard返回false。

    this.router.navigate(['/user/admin/editor'])
          .then(() =>{
            console.log('then block');
            
          })
          .catch(() => {
            console.log('catch block');
            
          })
          .finally(() => {
            console.log('final block');
            
          });
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Octavian Mărculescu    2 年前

    你可以订阅 the router events ,更具体地说,是 NavigationCancel 活动:

    this.router.events.pipe(
      filter(e => e instanceof NavigationCancel)
    ).subscribe(() => {
      console.log('navigation was cancelled');
    })