Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

uni-app数据存vuex
onLoad需要马上获取vuex里的值然后请求数据,但是onload不能马上取到值要等一会?每次都要setTimeout一下 为什么呢?


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

1 Answer

使用asyncawait处理异步请求即可,在onLoad就可以获取数据了;

async onLoad() {
    let res = await this.getData();
    console.log('接口数据:', res)
}

methods: {
    getData() {
        new Promise((resolve, reject) => {
            接口().then(res => {
                resolve(res)
            }).catch(error => {
                reject(error)
            })
        })
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...