https://cn.vuejs.org/v2/guide...
为了解决这个问题,我们采用一个简单的 store 模式:
var store = {
debug: true,
state: {
message: 'Hello!'
},
setMessageAction (newValue) {
if (this.debug) console.log('setMessageAction triggered with', newValue)
this.state.message = newValue
},
clearMessageAction () {
if (this.debug) console.log('clearMessageAction triggered')
this.state.message = ''
}
}
var vmA = new Vue({
data: {
privateState: {},
sharedState: store.state
}
})
var vmB = new Vue({
data: {
privateState: {},
sharedState: store.state
}
})
这个例子不完整没明白怎么用
例子其实已经很完整了,如上你在vmA和vmB调用了store.state,就相当于把公有的状态对象{message:'Hello'}赋予了他们,当你调用store.setMessageAction('world')时,你就会发现,vmA和vmB的sharedState由Hello变成了world了,所以可以达到公用状态