export class HttpClientHelper{
static baseURL: string = 'http://localhost:8080/myApp';
}
this.http.get(`${HttpClientHelper.baseURL}/api/items`);//in your service class
2 . 具有类属性的基类,因此任何新服务都应该扩展它:
export class BackendBaseService {
baseURL: string = 'http://localhost:8080/myApp';
constructor(){}
}
@Injectable()
export class ItemsService extends BackendBaseService{
constructor(private http: HttpClient){
super();
}
public listAll(): Observable<any>{
return this.http.get(`${this.baseURL}/api/items`);
}
}
@Injectable()
export class ApiHttpClient extends HttpClient {
public baseUrl: string;
public constructor(handler: HttpHandler) {
super(handler);
// Get base url from wherever you like, or provision ApiHttpClient in your AppComponent or some other high level
// component and set the baseUrl there.
this.baseUrl = '/api/';
}
public get(url: string, options?: Object): Observable<any> {
url = this.baseUrl + url;
return super.get(url, options);
}
}
6 回答
使用新的HttpClient拦截器 .
Create a proper injectable that implements HttpInterceptor:
HttpInterceptor可以克隆请求并根据需要进行更改,在这种情况下,我为所有http请求定义了一个默认路径 .
Provide the HttpClientModule with the following configurations:
现在您的所有请求都将以
your-api-url/
开头根据
TheUnreal
非常有用的答案,可以编写拦截器以通过DI获取基本URL:BASE_API_URL
可以由应用程序模块提供:其中
environment
是CLI在生成项目时自动创建的对象:你不一定需要一个带有HttpClient的 base URL ,文档说你只需要指定请求的api部分,如果你正在调用同一个服务器它就像这样直截了当:
this.http.get('/api/items').subscribe(data => { ...
但是,您可以根据需要或指定基本URL .
我有2条建议:
1 . 具有静态类属性的辅助类 .
2 . 具有类属性的基类,因此任何新服务都应该扩展它:
}
我认为没有默认的方法来做到这一点 . 做HttpService并在里面你可以定义你的默认URL的属性,并使用你的属性URL调用http.get和其他人的方法 . 然后注入HttpService而不是HttpClient
为什么不创建一个具有可配置baseUrl的HttpClient子类?这样,如果您的应用程序需要与多个服务通信,您可以为每个服务使用不同的子类,或者创建单个子类的多个实例,每个子类具有不同的配置 .
摘自Visual Studio 2017 asp.net核心webapi角度示例应用程序 .
包括Main.ts中的以下行
在您的组件中
我的完整main.ts代码如下所示
我的组件代码如下所示