async 函数的使用

下面的函数用async函数该如何写,请指教
methods:{
getList(){
this.listLoading=true
fetchList(this.listQuery).then(response=>{
this.list=response.data.items
this.total=response.data.total
//Justtosimulatethetimeoftherequest
setTimeout(()=>{
this.listLoading=false
},1.5*1000)
})
},
//fetchList
exportfunctionfetchList(query){
returnrequest({
url:'/article/list',
method:'get',
params:query
})
}
//request
importaxiosfrom'axios'
import{Message}from'element-ui'
importstorefrom'@/store'
import{getToken}from'@/utils/auth'
//createanaxiosinstance
constservice=axios.create({
baseURL:process.env.BASE_API,//api的base_url
timeout:5000//requesttimeout
})
//requestinterceptor
service.interceptors.request.use(config=>{
//Dosomethingbeforerequestissent
if(store.getters.token){
//让每个请求携带token--['X-Token']为自定义key请根据实际情况自行修改
config.headers['X-Token']=getToken()
}
returnconfig
},error=>{
//Dosomethingwithrequesterror
console.log(error)//fordebug
Promise.reject(error)
})
//responeinterceptor
service.interceptors.response.use(
response=>response,
error=>{
console.log('err'+error)//fordebug
Message({
message:error.message,
type:'error',
duration:5*1000
})
returnPromise.reject(error)
})
exportdefaultservice
MM们
浏览 691回答 2
2回答

慕哥9229398

asyncgetList(){this.listLoading=trueletresponse=awaitfetchList(this.listQuery)this.list=response.data.itemsthis.total=response.data.totalsetTimeout(()=>{this.listLoading=false},1.5*1000)},

RISEBY

varself=this;(asyncfunction(){constresponse=awaitfetchList(self.listQuery);self.list=response.data.itemsself.total=response.data.totalsetTimeout(()=>{self.listLoading=false},1.5*1000)})()根据补充的代码改成这样⬇️asyncgetList(){constresponse=awaitfetchList(this.listQuery);this.list=response.data.itemsthis.total=response.data.totalsetTimeout(()=>{this.listLoading=false},1.5*1000)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript