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

角度路由异常处理错误处理程序

  •  0
  • Prajwal  · 技术社区  · 6 年前

    如何处理导航错误 ErrorHandler

    我试着用以下条件来检查 Error NavigationError 但它说的是假的。

    export class AppErrorHandler implements ErrorHandler {
    
      constructor() { }
    
      handleError(error: Error) {
    if (error instanceof NavigationError) {
          //do something here, may be notify or redirect to error page.
        }
    }
    

    如何处理路由器导航错误 错误处理程序

    如果类型正确,上述条件是否有效?如果是,我应该检查哪种类型?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Damir Beylkhanov    6 年前

    @普拉杰瓦尔

    尝试使用下一种方法来处理错误,它应该适用于99.9%=):

    import { Router, NavigationError } from '@angular/router';
    
     constructor( private router: Router) {
    this.router.events.filter(e => e instanceof NavigationError).subscribe(e => {
      console.log(`NAV ERROR -> ${e}`);
    });
    

    }

    结果:

    The right relative path is ./home, but I have tried to go on ./home2