Service 角度2–;可注入服务不可用
我的angular2应用程序中有一个名为Service 角度2–;可注入服务不可用,service,typescript,angular,injectable,Service,Typescript,Angular,Injectable,我的angular2应用程序中有一个名为HttpClient的服务,该服务应该为应用程序发送到我的端点的每个请求添加一个授权头 import { Injectable } from '@angular/core'; import { Headers, Http, Response, } from '@angular/http'; import { Router } from '@angular/router'; import { ErrorService } from '../../s
HttpClient
的服务,该服务应该为应用程序发送到我的端点的每个请求添加一个授权头
import { Injectable } from '@angular/core';
import { Headers, Http, Response, } from '@angular/http';
import { Router } from '@angular/router';
import { ErrorService } from '../../services/error/error.service'
@Injectable()
export class HttpClient {
private token: string;
private error: any;
private webApi = 'http://localhost:8080/api/v1/'; // Url to web api
constructor(
private http: Http,
private router: Router,
private errorService: ErrorService) { }
get(url: string): Promise<Response> {
return this.http.get(this.webApi + url, this.createAuthorizationHeader())
.toPromise()
.catch((e) => this.handleError(e));
}
post(url: string, data: any): Promise<Response> {
return this.http.post(this.webApi + url, JSON.stringify(data), this.createAuthorizationHeader())
.toPromise()
.catch((e) => this.handleError(e));
}
put(url: string): Promise<Response> {
return this.http.get(this.webApi + url, this.createAuthorizationHeader())
.toPromise()
.catch((e) => this.handleError(e));
}
delete(url: string): Promise<Response> {
return this.http.delete(this.webApi + url, this.createAuthorizationHeader())
.toPromise()
.catch((e) => this.handleError(e));
}
private handleError (error: any) {
var status: number = error.status;
if (status == 415) {
this.errorService.setError(error);
}
let errMsg = (error.message)
? error.message
: status
? `${status} - ${error.statusText}`
: 'Server error';
console.error(errMsg); // log to console instead
return Promise.reject(errMsg);
}
private createAuthorizationHeader() {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
if (localStorage.getItem('token'))
this.token = localStorage.getItem('token');
headers.append('Authorization', 'Bearer ' + this.token);
return headers;
}
}
这些服务将在我的main.ts中启动
...
import { ErrorService } from './services/error/error.service';
import { HttpClient } from './services/http/http.service';
...
bootstrap(AppComponent, [
appRouterProviders,
HTTP_PROVIDERS,
ErrorService,
HttpClient,
....
]);
现在我想在标题组件中显示此错误。因此,超时发生了一个错误。这个错误将显示在我的标题中的一个单独的框中
问题是在HttpClient.handleError
中调用的ErrorService.setError(error)
方法甚至不会被激发
.catch(this.handleError);
应该是
.catch((e) => this.handleError(e));
保留此的范围。
其中(e)
是参数列表
或者,您可以使用
.catch(this.handleError.bind(this));
应该是
.catch((e) => this.handleError(e));
保留此的范围。
其中(e)
是参数列表
或者,您可以使用
.catch(this.handleError.bind(this));
没有做任何改变。
ErrorService.setError(error)
方法仍然没有被调用。调用的是handleError()
是()`是的。顺便说一句,catch(this.handleError)对我来说很好。我在其他地方发现了这个错误,所以这个问题不再有任何意义了。你一看完这篇文章,我就删除这个问题。当然,请继续。谢谢你的反馈。没有做任何更改。ErrorService.setError(error)
方法仍然没有被调用。调用的是handleError()
是()`是的。顺便说一句,catch(this.handleError)对我来说很好。我在其他地方发现了这个错误,所以这个问题不再有任何意义了。你一看完这篇文章,我就删除这个问题。当然,请继续。谢谢你的反馈。