I'm new to vue, so I'm probably making a rookie error.
I have a root vue element - raptor.js:
const Component = { el: '#app', store, data: { productList: store.state.productlist }, beforeCreate: function () { return store.dispatch('getProductList', 'getTrendingBrands'); }, updated: function (){ console.log(111); startSlider(); } }; const vm = new Vue(Component);
Using this template
<div class="grid-module-single popular-products" id="app"> <div class="row"> <div class="popular-items-slick col-xs-12"> <div v-for="product in productList"> ... </div> </div> </div>
My store is very simple store/index.js:
import Vue from 'vue'; import Vuex from 'vuex'; import model from '../../utilities/model'; Vue.use(Vuex); export default new Vuex.Store({ state: { productlist: [] }, mutations: { setProductList(state, data) { state.productlist = data; } }, actions: { getProductList({ commit }, action) { return model.products().then(data => commit('setProductList', data)); } } });
In my vuex devtool, I can see, that the store is being updated https://www.screencast.com/t/UGbw7JyHS3
but my component is not being updated: https://www.screencast.com/t/KhXQrePEd
Question:
I can see from the devtools, that my code is working. The store is being updated with data. My component is not being updated,however. I thought it was enough just to add this in the data property on the component:
data: { productList: store.state.productlist }
but apparently the data object doesn't seem to be automatically synced with the store. So either I'm doing a complete vue no-no somewhere, or I need to tweak the code a bit. Anyway can anyone help me in the right direction.
Thanks a lot.
Vuex stores are reactive. When Vue components retrieve state from it, they will reactively and efficiently update if the store's state changes. You cannot directly mutate the store's state. The only way to change a store's state is by explicitly committing mutations.
One of Vue's best features is reactivity. You change a variable, and Vue automatically updates what is rendered to the page. However, not everything in Vue is reactive by default.
Mapping in Vuex enables you to bind any of the state's properties, like getters, mutations, actions, or state, to a computed property in a component and use data directly from the state. Although we can get the job done with this. $store.state.user.data.name , we can use a map helper to simplify it to this.
The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.
UPDATE
Figured it out myself. Just had to replace the components data part with a computed method:
data:
data: { productList: store.state.productlist }
and replace it with.
computed: { productList () { return store.state.productlist; } },
data only work once on component before render, so you can use computed instead. like above answer, or you can use mapstate
import {mapState} from 'vuex' ... computed: mapState({ productList: state => state.productList })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With