Using mpvue to develop small programs how to define global variables

After we created the mpvue project, we found src/main.js and added a line of code behind it.

(Note: cannot be added before const app = new Vue (App).

Vue.prototype.globalData = getApp().globalData

This line of code means that the global variables of the applet are mounted on the Vue prototype so that we can get the global data on different pages

Different pages can be acquired or modified by this.globalData.

For example, I need to store some basic information of users.

this.globalData.userInfo = {name:aaa}

Different pages get the same way.

console.log(this.globalData.userInfo.name)

modify

this.globalData.userInfo.name = ‘bbb’

Attention:When the applet is started, the code in the Vue periodic function created in different pages will be executed once again. Therefore, in the created periodic function of Vue, try not to request too many interfaces. It is placed in the periodic function onLoad of the applet.

 

Leave a Reply

Your email address will not be published. Required fields are marked *