小项目用vuex的弊端:
- 引起不必要的项目体积
- 引起复杂的状态管理麻烦
如果是小项目,需要共享的状态不是很多,可以舍弃vuex,用别的方案替代:
方案1:bus
//vue原型链挂载总线
Vue.prototype.bus = new Vue();
//子组件发送数据
this.bus.$emit("change",data);
//子组件接收数据
this.bus.$on("change",function(data){
})
方案2:简易自制store
import Vue from "vue"
export const store = Vue.observable({a:33})
export const mutation = {
change(v){
store.a = v || ''
}
}
// 使用
computed: {
msg() {
return store.a;
}
},
methods: {
update(v) {
mutation.change(v)
}
},
网友评论