美文网首页
main.js中的异步async等待,vue,uniapp

main.js中的异步async等待,vue,uniapp

作者: wyc0859 | 来源:发表于2020-07-05 09:33 被阅读0次

vue或uniapp在main.js中如何获取请求后再往下执行,百度结果:vue可以用路由钩子。
但uniapp是没有路由钩子的,想了想,把main.js中的vue实例化、启动包裹在async函数中不就ok,测试了下真OK

import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false

const start =async ()=>{
    const r = await uni.request({
        url: ''
    }).then(data => { 
        return "123"
    }) 
    console.log("r",r)
    Vue.prototype.$name = r  //异步数据绑定到全局
    App.mpType = 'app' 
    const app = new Vue({
        ...App
    })
    app.$mount()
} 
start()

index.vue成功获取异步数据

<template>
    <view class="content">
        index---{{$name}}
    </view>
</template>

研究了下uniapp的globalData,想一开始就获取异步数据,但未能成功

相关文章

网友评论

      本文标题:main.js中的异步async等待,vue,uniapp

      本文链接:https://www.haomeiwen.com/subject/wcgmqktx.html