简体   繁体   English

Vuex突变状态变量不起作用

[英]Vuex Mutating a state variable not working

I'm trying to update my contacts when a different brand is selected. I'm trying to update my contacts when a different brand is selected. When I select a new brand, the contacts should be updated.当我选择一个新品牌时,应该更新联系人。 So I clear my array of the contacts of the brand and then fill it again.所以我清除了我的品牌联系人数组,然后再次填写。

Somehow though, the clearing of the array doesn't work in my Vuex setup.但不知何故,数组的清除在我的 Vuex 设置中不起作用。 Is there anyone who knows why?有没有人知道为什么?

This is my Store file:这是我的商店文件:

export default {
    state: {
        brands: Array(), //Might be used later on, if not, remove.
        brandsForDropdown: Array(),
        brandContactsForDropdown: Array(),
    },
    getters: {
      brands: state => {
        return state.brands;
      },
      brandsForDropdown: state => {
        return state.brandsForDropdown
      },
      brandContactsForDropdown: state => {
        return state.brandContactsForDropdown
      }
    },
    actions: {
        getBrands({state, commit}) {
            Vue.http.get(process.env.VUE_APP_API_SERVER + '/brands').then(response => {
                if(response.body.length > 0) {
                    for (var i = 0; i < response.body.length; i++) { 
                        commit('pushBrands', {"name" : response.body[i].name, "value" : response.body[i].id})
                    }
                }
              }, response => {
                // error callback
              });
        },
        getBrandContacts({state, commit}, payload) {
            //commit('resetBrandContacts')
            Vue.http.get(process.env.VUE_APP_API_SERVER + '/brands/contacts/' + payload.value).then(response => {
                if(response.body.length > 0) {
                    let newArray = [];
                    for (var i = 0; i < response.body.length; i++) { 
                        newArray.push({"name" : response.body[i].firstname + " " + response.body[i].surname, "value" : response.body[i].id})
                    }
                    commit('pushBrandContact', newArray)
                }
              }, response => {
                // error callback
              });
        }
    },
    mutations: {
        pushBrands(state, payload) {
            state.brandsForDropdown.push(payload)
        },
        resetBrands(state) {
            state.brandsForDropdown = []
        },
        resetBrandContacts(state) {
            state.brandContactsForDropdown = []
        },
        pushBrandContact(state, payload) {
            console.log(payload)
            state.brandContactsForDropdown = payload
            console.log(state.brandContactsForDropdown)
        }
    }
}

This is my full component code:这是我的完整组件代码:

<script>
export default {
  data () {
    return {
      productName: null,
      productBrand: null,
      brands: this.$store.getters.brandsForDropdown,
      selectedBrand: null,
      brandContacts: this.$store.getters.brandContactsForDropdown,
      selectedContacts: null,
    }
  },
  computed: {
  },
  watch: {
      selectedBrand: function() {
          if(this.selectedBrand != null) {
              this.$store.dispatch('getBrandContacts', this.selectedBrand)
              //this.brandContacts = this.$store.getters.brandContactsForDropdown
          }
          console.log(this.brandContacts);
      }
  },
  methods: {
  },
  mounted: function() {
  this.$store.dispatch('getBrands')
  }
}
</script>

And up there is my full Vuex module.上面是我完整的 Vuex 模块。

I assume you reached one of the Gotchas :我假设您达到了其中一个陷阱

I'm not sure if this is the case where you update the array and it will not detect the change, but the best way to update the array would be to place a new array instead of adding to it:我不确定这是否是您更新数组并且它不会检测到更改的情况,但是更新数组的最佳方法是放置一个新数组而不是添加到它:

getBrandContacts({state, commit}, payload) {
  Vue.http.get(process.env.VUE_APP_API_SERVER + '/brands/contacts/' + 
               payload.value).then(response => {
    if(response.body.length > 0) {
      let newArray = []
      for (var i = 0; i < response.body.length; i++) { 
        newArray.push({"name" : 
                       response.body[i].firstname + " " + response.body[i].surname, "value" : 
                       response.body[i].id})
      }
      commit('pushBrandContact', newArray)
    }
  }, response => {
    // error callback
  });
}

and in mutations:在突变中:

pushBrandContact(state, payload) {
  state.brandContactsForDropdown = payload
}

It is also not clear how you work with the data inside the component, is it a data?也不清楚您如何处理组件内的数据,它是数据吗? or in computed?或计算? sharing this code will help.共享此代码将有所帮助。 You also say the array doesn't clear, what you mean?你还说数组不清晰,你的意思是什么? it have the old values?它有旧的价值观吗? in the vuex state?在 vuex 状态下? on in your component?在您的组件中? a lot of info to help is missing.缺少很多可以提供帮助的信息。

Update更新

The data properties are getting updated only on init of the component, and when you manually update it ( this.data = newData ), they will not be updated when Vuex is updated.数据属性仅在组件初始化时更新,当您手动更新它时( this.data = newData ),更新 V​​uex 时它们不会更新。

You need to move the brandContacts: this.$store.getters.brandContactsForDropdown from the data object to the computed object like this: brandContacts(){ this.$store.state.brandContactsForDropdown }您需要将brandContacts: this.$store.getters.brandContactsForDropdown从数据对象移动到计算对象,如下所示: brandContacts(){ this.$store.state.brandContactsForDropdown }

This will force vue to update when the brandContactsForDropdown property in vuex updates.当 vuex 中的brandContactsForDropdown属性更新时,这将强制 vue 更新。

https://v2.vuejs.org/v2/guide/computed.html https://v2.vuejs.org/v2/guide/computed.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM