2

I am new to vuejs I want re-build my v-for loop after update vuex object. see following example code.

<div class="row search-result-row" v-for="report in reports" :key="report">
   <p>{{ report.description }}</p>
</div> 

here is my vuex object called globalReports. when I equal globalReports to reports it doesn't work.

computed: {
    updateReports: function() {
        return  this.reports = this.$store.state.globalReports;
    }
},

How can I do this without page reload?

1
  • It's unclear why updateReports is a computed property. Shouldn't it be in methods? It might be the case that updateReports is never referenced in the DOM, thereby never "computed" per se and does not execute. VueJS is reactive such that whenever this.reports is updated, your DOM will be updated. Commented Apr 3, 2018 at 8:08

3 Answers 3

2

Try with {{updateReports}}. Computed will not be executed util it is monitored or called, just called {{updateReports}}

<div class="row search-result-row" v-for="report in reports" :key="report">
   <p>{{ report.description }}</p>
</div>
{{updateReports}}

And don't return anything just update/Assign value of this.reports

computed: {
    updateReports: function() {
        this.reports = this.$store.state.globalReports;
    }
},
Sign up to request clarification or add additional context in comments.

1 Comment

Computed properties are just like getters . They are converted into normal properties of vue with this getter, so its better to return a value instead of assigning to a data property, and can just be used in templates like normal data properties. So updateReports can just return the state globalReports and used in as v-for="report in updateReports"
2

by using mapState, you can map the value of globalReports to reports automatically.

Everytime globalReports change, reports gets updated automatically, and the rebuild will happen automatically when it gets updated.

<script>
import { mapState } from "vuex";
export default {
  computed: mapState({
    reports: "globalReports"
  })
};
</script>

Comments

2

Vuex is reactive so when you update the state,this change will affect all components where you use the state properties.To be more specific:

I will show you an example:

    //VUEX STORE
state: {
    property1
},
getters: {
    getProperty1(state) {
        return state.property1
    }
},
mutations: {
    setProperty1(state, payload) {
        state.property1 = payload
    }
},
actions: {
    changeProperty1({commit}, payload) {
        commit('setProperty1', payload)
    }
}

And below is the component in which you interact with state

<template>
    <p>this is from state of store {{ getProperty1 }</p>
    <input type="text" v-model="value"> 
    <button @click="changeState">Sumbit</button>
</template>
<script>
    export default {
        data() {
            return {
                value: ''
            }
        },
        computed: {
            getProperty1() {
                return this.$store.getters.getProperty1
            }
        },
        methods: {
            changeState() {
                this.$store.dispatch('changeProperty1', this.value)
            }
        }
    }
</script>

Getters are to get the state properties Mutations to change the state properties Actions to perform async code and then to call mutations to change the state

For more please visit vuex docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.