1、先再app.vue 中定义一个全局数据对象globalData,再对象里定义好相应的属性与属性值, 在methods创建watch方法
globalData: {
msgNumber: 0, // 消息条数
},
methods: {
watch: function(method, istr) {
var obj = this.globalData
Object.defineProperty(obj, istr, {
configurable: true,
enumerable: true,
set: function(value) {
this._consumerGoodsStatus = value
method(value)
},
get: function(value) {
return this._consumerGoodsStatus
}
})
}
},
2、在需要用到的页面或者组件中引入
created() {
const App = getApp()
App.watch(this.getMsgNumber,'msgNumber') // msgNumber是globalData中定义的属性
},