Javascript ionic3如何在ErrorHandlerService中注入服务/提供者
问题: ======= 当我在ErrorHandlerService中注入ConfigService来祝酒时,给我一个错误:ErrorHandlerService.ts:36 发生错误:Error:Uncaught(承诺中):TypeError:无法读取未定义的属性“\u appLog”Javascript ionic3如何在ErrorHandlerService中注入服务/提供者,javascript,angular,ionic-framework,Javascript,Angular,Ionic Framework,问题: ======= 当我在ErrorHandlerService中注入ConfigService来祝酒时,给我一个错误:ErrorHandlerService.ts:36 发生错误:Error:Uncaught(承诺中):TypeError:无法读取未定义的属性“\u appLog” export class ErrorHandlerService implements ErrorHandler { private _appLog: ConfigService;
export class ErrorHandlerService implements ErrorHandler {
private _appLog: ConfigService;
constructor (private injector: Injector) {
setTimeout(() => {
this._appLog = injector.get(ConfigService);
}, 0);
}
handleError(error: Error | HttpErrorResponse) {
if (error instanceof HttpErrorResponse) {
if (!navigator.onLine) {
console.log('Please, check your internet connection');
} else if(error.status === 401){
console.log(error.message);
this._appLog.presentToast();
}else {
console.log(error.message);
}
} else {
console.log('Not Http Error');
}
// Log the error anyway
console.error('It happens: ', error);
}
}
@Injectable()
export class ConfigService {
constructor(private storage: Storage){
}
presentToast(){
console.log("test");
}
}
getUsers(){
let promise = new Promise((resolve, reject) => {
let apiURL = `${this.configS.apiRootV1}/users`;
this.http.get(apiURL).toPromise()
.then((res) => {
console.log(res);
resolve(res);
}).catch(this.errorHandlerService.handleError);
});
return promise;
}
在handleError函数中,“this”未定义。尝试使用“绑定”发送“this”的值:
.catch(this.errorHandlerService.handleError.bind(this.errorHandlerService));
在handleError函数中,“this”未定义。尝试使用“绑定”发送“this”的值:
.catch(this.errorHandlerService.handleError.bind(this.errorHandlerService));