下面的函数用async函数该如何写,请指教methods:{getList(){this.listLoading=truefetchList(this.listQuery).then(response=>{this.list=response.data.itemsthis.total=response.data.total//JusttosimulatethetimeoftherequestsetTimeout(()=>{this.listLoading=false},1.5*1000)})},//fetchListexportfunctionfetchList(query){returnrequest({url:'/article/list',method:'get',params:query})}//requestimportaxiosfrom'axios'import{Message}from'element-ui'importstorefrom'@/store'import{getToken}from'@/utils/auth'//createanaxiosinstanceconstservice=axios.create({baseURL:process.env.BASE_API,//api的base_urltimeout:5000//requesttimeout})//requestinterceptorservice.interceptors.request.use(config=>{//Dosomethingbeforerequestissentif(store.getters.token){//让每个请求携带token--['X-Token']为自定义key请根据实际情况自行修改config.headers['X-Token']=getToken()}returnconfig},error=>{//Dosomethingwithrequesterrorconsole.log(error)//fordebugPromise.reject(error)})//responeinterceptorservice.interceptors.response.use(response=>response,error=>{console.log('err'+error)//fordebugMessage({message:error.message,type:'error',duration:5*1000})returnPromise.reject(error)})exportdefaultservice
2 回答
慕哥9229398
TA贡献1877条经验 获得超6个赞
asyncgetList(){this.listLoading=trueletresponse=awaitfetchList(this.listQuery)this.list=response.data.itemsthis.total=response.data.totalsetTimeout(()=>{this.listLoading=false},1.5*1000)},
RISEBY
TA贡献1856条经验 获得超5个赞
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)}
添加回答
举报
0/150
提交
取消