Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

关于 async await的问题

问题:页面刷新的时候发现有时候遮罩不显示,然后对代码添加了 aysnc,await 结果解决了。看注释
使用的Vue 脚手架,直接贴代码:

//main.js(部分代码)
// axios 封装和 api 封装
import request from './assets/js/request.js'
Vue.prototype.request = request;

let vueExample = new Vue({
  router,
  store,
  vuetify,
  render: h => h(App)
}).$mount('#app')
export default vueExample;


//request.js(部分代码) 
async API_classification_select(callback,errback) {
    //问题在这里,如果我不用 async await 的话,刷新页面会出现 vueExample 未定义的报错情况
    //await 等待的也不是一个 promise,为什么这么执行没报错。    
    //感觉等价于以下代码
    /**
        let a;
        function fn(){
           return new Promise((resolve)=>{
                setTimeout(()=>{
                    a = 1;
                },2000);
                if(a){
                    resolve();
                }
           })
        }
        fn();
    **/
    await vueExample;
    //显示 遮罩加载中
    vueExample.$store.commit('showOverlay');
    axios.post(baseURL.api_host + '/classification/select')
        .then(function(response) {
            console.log('查分类响应: ', response.data)
            var r = response.data;
            //隐藏
            vueExample.$store.commit('hideOverlay');
            if (r.status == 0) {
                callback(r)
            } else {
                !!errback && errback() || vueExample.$toast(r.message);
                
            }
        })
        .catch(function(error) {
            console.log(error)
        })
},

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

import支持 await关键字,详见:
https://developer.mozilla.org...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...