假设我有一个axios
函数的包装器 - 应该在每个ajax查询上实现,所以我想保留代码DRY。像那样:
import axios from "axios"
import NProgress from "nprogress"
const query = (url, options) => {
NProgress.start()
return axios({
url: url,
method: options.method || "GET",
data: options.data || {}
}).then(() => {
NProgress.done()
})
}
export default query
问题在于,如果我将.then
解析器添加到query()
,则不会发生任何事情!像那样:
从" ./ query.js"
导入查询query("something", {}).then(() => { console.log("This will never logged") })
如何向.then()
功能添加其他query()
?
答案 0 :(得分:1)
回复一些东西!
const query = (url, options) => {
NProgress.start()
return axios({
url: url,
method: options.method || "GET",
data: options.data || {}
}).then((response) => {
NProgress.done()
return response // change is here
})
}