vue await async

1. await, async使用
函数头单独封装出去
views/methods.js页面
import { one } from 'api/general.js';
async function change() {
let data = {},
Obj = {},
key = '';

data = await one();

for (key in data.data) {
Obj[key] = data.data[key];
}
return Obj;
}

export {
change
}


------------------------------
api/general.js页面
//先执行的函数
function one() {
return fetch({
url: url,
method: 'get',
})
}

export {
one
}


//传参的:
//function one(data) {
   // return fetch({
// url: url,
// method: 'get',
// data: 'data'
// })
//}


------------------------
页面调用封装的函数头
import { change } from 'views/methods.js'

export default { 
     data() {
changeObj : {}
}
     created() {
change().then(res => {
this.changeObj = res;
});
    }

猜你喜欢

转载自www.cnblogs.com/namehou/p/11730694.html