angular中的http:
参考:
官网:https://angular.cn/api/common/http/HttpClient#get
个人:
https://blog.csdn.net/It_rod/article/details/79811009
https://blog.csdn.net/u013451157/article/details/79519719
- get请求
// 请求参数:
// headers:请求头;observe:res里有响应头;params:get请求的body参数;
const options = {
headers: new HttpHeaders({
'currentApp': 'currentApp',
'currentCode': 'currentCode'
}),
observe: 'response',
params: params,
};
this.http.get('/serviceName/api/user/get-user-info', options).subscribe(res => {
console.log('获取用户信息接口:', res);
console.log('body:', res.body); // 请求的响应体body
console.log(res.headers.get('status_code')); // 获取响应头里面的status_code值
});
- post请求
// params单独第二个参数使用:
const options = {
headers: new HttpHeaders({
'currentApp': 'currentApp-login',
'currentCode': 'currentCode-login'
}),
observe: 'response',
// params: params,
};
this.http.post('/auth/login', params, options).subscribe(res => {
console.log('登录接口:', res);
});
注:如果params放在options里面,那么参数会自动拼写在接口后面;eg:
/auth/login?username=uSqyRKsMrkpg0X5tG7CQXg==&password=gBV3H+S/xW0=
即:在接口后面拼写分页的方法。
ts语法检查错误:
Argument of type
'{ 'observe': string; 'params': {}; 'headers': HttpHeaders; }'
is not assignable to parameter of type
'{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
Types of property 'observe' are incompatible.
Type 'string' is not assignable to type '"body"'.
==>这个错误在编译的时候会报错;没有找到好的解决办法;
so:我把参数放在函数内部,不提出来就ok了,eg:
// options不提出来,就是直接放在函数 内。
this.http.get('/serviceName/api/user/get-user-info', {
headers: new HttpHeaders({
'currentApp': 'currentApp',
'currentCode': 'currentCode'
}),
observe: 'response',
params: params,
}).subscribe(res => {
console.log('获取用户信息接口:', res);
console.log('body:', res.body); // 请求的响应体body
console.log(res.headers.get('status_code')); // 获取响应头里面的status_code值
});
继续把http放入base-service中,请参考:
https://blog.csdn.net/weixin_42995876/article/details/95352550