有什么区别:
this.currentLanguage = this.$store.getters.currentLanguage;
这是给 getter 的:
this.currentLanguage = this.$store.state.currentLanguage;
还有这个:
this.$store.commit('setLanguages', this.languages);
在突变中给了我相同的结果:
this.$store.state.languages = this.languages;
在这两种情况下,我都得到了我想要的。直接获取值(value)更容易,因为我不需要编写 getter/mutation。 我为什么要使用 getter/mutations?
最佳答案
在这种情况下您不需要使用 setter/getter 。 getter 就像常规 Vue 组件上的计算属性,因此只有当数据是其他数据的更复杂后代时才需要它们。
然而,你想使用突变,因为即使它看起来像 this.$store.state.languages = this.languages
正在工作,它也不一定更新它 正确。如果你有依赖于商店的其他组件,它可能不会收到新数据,因为它还没有经历完整的变异过程。
关于vue.js - 如果直接获取/设置值,为什么我应该使用 getters 和 mutations 而不是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52483142/