Skip to content

Commit

Permalink
feat(AxiosRequestTemplate): useAxios内'AxiosRequestTemplate.axios = ax…
Browse files Browse the repository at this point in the history
…ios;'改为'this.axios = axios;'也就是说子类也可以设置axios能够覆盖父类的axios,init时如果检测到未传入axios将抛出错误
  • Loading branch information
mengxinssfd committed Oct 27, 2022
1 parent a52da43 commit ad0f9dd
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/AxiosRequestTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class AxiosRequestTemplate<
* 用axios作为请求工具时必须调用该方法
*/
static useAxios(axios: AxiosStatic) {
AxiosRequestTemplate.axios = axios;
this.axios = axios;
}

/**
Expand All @@ -25,8 +25,14 @@ export class AxiosRequestTemplate<
protected axiosIns!: AxiosInstance;

protected init() {
const proto = Object.getPrototypeOf(this).constructor;
const axios = proto.axios;
if (!axios || typeof axios.create !== 'function') {
throw new Error('使用前必须先传入axios,调用useAxios(axios)');
}

// 1、保存基础配置
this.axiosIns = AxiosRequestTemplate.axios.create(this.globalConfigs.requestConfig);
this.axiosIns = axios.create(this.globalConfigs.requestConfig);
super.init();
this.setInterceptors();
}
Expand All @@ -35,7 +41,7 @@ export class AxiosRequestTemplate<
* 获取拦截器
*/
protected get interceptors() {
return this.axiosIns?.interceptors;
return this.axiosIns.interceptors;
}

/**
Expand Down

0 comments on commit ad0f9dd

Please sign in to comment.