简体   繁体   English

router.navigate()无法正常工作且不会引发错误

[英]router.navigate() not working and not throwing error

I have this method : 我有这种方法:

  private redirect(path: string): void {
    this.router.navigate([path]);
  }

That is called in this one : 这就是所谓的:

  private onError(error: any): void {
    switch (error.status) {
      case 401: // Unauthorized.
        this.redirect('auth/login');
      break;
    }

That is called from this (this is the get from httpService) 从这里调用(这是从httpService获得的)

public get(url: string, requestOptions: RequestOptionsArgs): Observable<any> {
    this.requestInterceptor();
    return super.get(this.getFullUrl(url), this.requestOptions(requestOptions))
      .do((res: Response) => {
        if (res.ok) {
          this.onSuccess(res);
        }
      }, (error: any) => {
        this.onError(error);
      })
      .finally(() => {
        this.onFinally();
      });
  }

=> Navigate does not work, nothing happens, no error throwing no log. =>导航不起作用,什么也没有发生,没有错误也没有抛出日志。 I tried to log before and after the call. 我尝试在通话之前和之后进行登录。 I get the log before but not after... So there is a problem. 我在之前但未在之后获取日志...所以有问题。

I use the same navigate in other places with no issue but here I don't see why it would not work . 我在其他地方使用相同的导航没有问题,但是在这里我看不到为什么它不起作用。

Any Idea ? 任何想法 ?

EDIT : 编辑:

This is a service : 这是一项服务:

@Injectable()
export class HttpService extends Http {

  constructor(backend: ConnectionBackend,
    defaultOptions: RequestOptions,
    public router: Router,
    public alertService: AlertService) {
    super(backend, defaultOptions);
  }
  // Here the get() method
}

In the parent core.module there is a factory : 在父core.module中有一个工厂:

export function HttpFactory(backend: XHRBackend,
                            defaultOptions: RequestOptions,
                            router: Router,
                            alertService: AlertService): HttpService {
  return new HttpService(backend, defaultOptions, router, alertService);
}

The issue was that I forgot to add the proper dependencies during declaration : 问题是我忘了在声明期间添加适当的依赖项:

service,
{
      provide: HttpService,
      useFactory: HttpFactory,
      deps: [XHRBackend, RequestOptions, Router],
}

Router was missing Router丢失

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM