简化版 vuex ( Vue.observable )

全局变量
因为Vuex使用比较复杂, 在非大型项目中使用起来不方便, 为了简化传值操作, vue2.6之后新增此API

创建

main.js

import Vue from 'vue';

const obbStore = {

	state: Vue.observable({
		count: 0,
		name: '李四'
	}),

	mutations: {
		setCount(count) {
			obbStore.state.count = count
		},
		changeName(name) {
			obbStore.state.name = name
		}
	}
}

Vue.prototype.$obbStore = obbStore;

使用

this.$obbStore.mutations.setCount(110)
posted @ 2021-11-12 15:35  极学者  阅读(58)  评论(0)    收藏  举报