我有这个方法:
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();
});
}
=&GT;导航不起作用,没有任何反应,没有错误抛出没有日志。我试着在通话前后记录。我之前得到的是日志而不是之后......所以有一个问题。
我在其他地方使用相同的导航没有问题,但在这里我不明白为什么它不起作用。
任何想法?
编辑:
这是一项服务:
@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);
}
答案 0 :(得分:0)
问题是我忘了在声明期间添加适当的依赖项:
service,
{
provide: HttpService,
useFactory: HttpFactory,
deps: [XHRBackend, RequestOptions, Router],
}
Router
失踪