Obtain globalData from the app page of the uni-app development applet

In the process of using the uni-app framework to develop WeChat applets, I want to use globalData to set global variables. In the app.vue page, get globalData and use this.globalData.xxx directly, but the console reports an error saying that the variable is undefined. Search Many articles say that they are obtained in this way, but I didn't understand how they obtained it. As a result, I found a bug for a long time, and then found that the globalData obtained on the app.vue page cannot be obtained directly, and an $options must be added; Calling on other pages should be obtained through getApp()

//App.vue在App.vue页面
<script>
    export default{
        globalData:{
            test:''
        },
        onlaunch:function(){
            //获取全局变量
            console.log(this.$options.globalData.test)
        }
    }
</script>

//other.vue在其他页面调用
<script>
    let App = getApp()
    export default{
        onload:function(){
            //获取全局变量
            console.log(App.globalData.test)
        }
    }
</script>

 

Guess you like

Origin blog.csdn.net/miao_yf/article/details/102915146
Recommended