【问题标题】:router.navigate() not working and not throwing errorrouter.navigate() 不工作且不抛出错误
【发布时间】:2017-08-09 12:19:54
【问题描述】:

我有这个方法:

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

这就是所谓的:

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

从这里调用(这是从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();
      });
  }

=> 导航不起作用,没有任何反应,没有错误,没有日志。我试图在通话前后记录。我之前得到了日志,但之后没有……所以有问题。

我在其他地方使用相同的导航没有问题,但在这里我不明白为什么它不起作用。

有什么想法吗?

编辑:

这是一项服务:

@Injectable()
export class HttpService extends Http {

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

在父 core.module 中有一个工厂:

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

【问题讨论】:

  • 您的重定向方法从未使用过
  • 不是很明显,我编辑了这个问题。我在case 401 中登录,但它从未在this.redirect() 之后。它只是停在那里没有任何日志或任何东西
  • 贴出你拥有的整个类/组件的代码以及如何注入router
  • 这是一个很好的建议,谢谢你,我解决了这个问题:)

标签: javascript angular reactjs typescript angular2-routing


【解决方案1】:

问题是我忘记在声明期间添加正确的依赖项:

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

Router 不见了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 2015-10-11
    • 1970-01-01
    相关资源
    最近更新 更多