uniapp the use of global variables globalData

When using uniapp development, you can define global variables in app.vue, the acquisition by globalData within no pages

1, the internal document written app.vue

<script>
    export default {
        globalData: {
            text: 'text'
        },
        onLaunch: function() {
            console.log('App Launch')
        },
        onShow: function() {
            console.log('App Show')
        },
        onHide: function() {
            console.log('App Hide')
        }
    }
</script>

<style>
    / * Each page Public CSS * / 
</ style>

2, written in each subordinate file

the onLoad () {
           var GlobalData = getApp () globalData.text;. // global variables 
          getApp () globalData.text = 'ABC';. // modify global variables 
          console.log (globalData);
          console.log(getApp().globalData.text);
        },

 3, should be noted that, if the need to obtain this global variable in app.vue, you need the following lines

onLaunch: function() {
            console.log(this.$scope.globalData.text)
            console.log('App Launch')
        },

 

Guess you like

Origin www.cnblogs.com/Alex-Song/p/12512358.html