0
import { Injectable } from '@angular/core';
import { Http, ConnectionBackend, Headers, RequestOptions, Request, Response, RequestOptionsArgs } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
@Injectable()
export class SecureHttpService extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
// return super.request(url, options);
return super.request(url, options).catch(this.catchErrors());
}
private catchErrors() {
return (res: Response) => {
if (res.status === 401 || res.status === 403) {
//nav.push(loginPage); I want to do something like this if status is 401
debugger;
}
return Observable.throw(res);
};
}
我試圖導航時,有401個錯誤狀態到登錄頁面,但作爲離子不允許被服務內部使用服務的登錄頁面。這怎麼能實現?導航到上獲得使用攔截器狀態401離子2
app.module.ts: -
providers: [
{
provide: Http,
useFactory: (backend:XHRBackend, defaultOptions:RequestOptions) => {
return new SecureHttpService(backend, defaultOptions);
},
deps: [XHRBackend, RequestOptions]
},
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
我已經這樣做了,我的攔截器正在運行。我想要做的就是導航到錯誤狀態401的登錄頁面,但是因爲我無法在我的服務中使用navcontroller,所以我如何才能登錄頁面 –
啊我看到了,目前在我的手機上,但是查看了ionics事件',可以讓您從您的服務中釋放出來,並在fe中傾聽該事件app.component(所以它總是執行)你可以在哪裏使用NavController – Ivaro18
事件服務也無法正常工作,它說無法讀取未定義的'publish'。 'private catchErrors(){(res:Response)=> {res.status === 401 || res.status === 403} this.ev.publish('setRoot',this。名稱); // this.navCtrl.push(HomePage); } return Observable.throw(res); }; }' –