vuex中mutaions注意事项

xiaoxiao2025-05-19  43

mutaion 遵循 vue 的响应式规则

当store的内容有所变化,监视状态的vue组件会自动更新,例如:

// store.js ... state: { username: '', } // userInfo.vue <template> <div> hello, {{username}} </div> </template> <script> ... data() { return { username: this.$store.state.username } } </script>

当store中的username改变,userInfo.vue 中的值会自动更新。

注意点

最好提前在你的 store 中初始化好所有所需属性。当需要在对象上添加新属性时,你应该 Vue.set(obj, 'newProp', 123) 或者 state.obj = { ...state.obj, newProp: 123 }
转载请注明原文地址: https://www.6miu.com/read-5030369.html

最新回复(0)