vue : async / await 的应用

比如有组数据是很多页面都要用的,我想把它写在一个js文件里作为公共方法。

public.js 

 1 import axios from 'axios';
 2 axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
 3 import { get_industrylist } from './api.js'
 4 
 5 export const getIndustryList = async function () {    
 6     let data = await new Promise((resolve)=>{
 7         axios.post(get_industrylist).then((res) => {
 8             console.log(res)
 9             if(res.data.code === 200){
10                 // console.log('getIndustryList', res.data.data.industrylist)
11                   resolve(res.data.data.industrylist)
12             }        
13         })
14     })
15     return data    
16 }

然后在某个页面里使用它。

Tanant.vue

1 ...
2 created(){
3     getIndustryList().then((v)=>{      
4       this.options = v      
5     })
6   },
7 ...

就是这样。

猜你喜欢

转载自www.cnblogs.com/foxcharon/p/9074182.html