简体   繁体   中英

View doesn't get updated after updates in store Vue js

I have Store , where I have actions and mutations

My action loadProducts calls the mutation setProducts , where I set values in state and it works fine

const actions = {
  loadProducts({ commit }, accountId ) {
    Products.where( { accountId: accountId } ).all().then((products) => {
      commit("setProducts", {accountId: accountId, products: products.data})
    });
  },
}
const mutations = {
  setProducts(state, {accountId, products}) {
    Vue.set(state, 'products', products);
  }
}

And then in my component I get the data, using mapGetters and computed

 computed: {
   ...mapGetters([
     'products'
   ]),
   products2() {
     console.log(this.products)
     return this.products;
   }}

Everything works, but then I try to update the data, it also works, I get updated state - in Vuex console I can see my updates state and new data, but the view itself won't be updated - so i Have to reload the page, to see the data appears. I also do it via actions, mutations and getters, in mutations it looks like this:

replace() {
   Vue.set(state, key, val);
}

Should you be using mapState instead of mapGetters to directly map the state products to your component? Do you have a getter called products that does some extra work?

...mapState([
  'products'
]),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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