【发布时间】: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