1 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
我建議像這樣包裝所有其余的 api 調(diào)用:
class RestApi {
constructor() {
this.client = axios.create({
baseURL: 'base-url-here'
});
}
async get(url, params = {}) {
return this.client.request({
type: 'get',
url: url,
params: params
})
}
async post(url, data = {}) {
return this.client.request({
type: 'post',
url: url,
data: data
})
}
async getById(id) {
return this.get(`Book/GetById/${id}`)
}
}
const api = new RestApi();
const response = await api.getById(1);
添加回答
舉報(bào)