P
P
pankratov_tolik2019-03-25 15:38:30
JavaScript
pankratov_tolik, 2019-03-25 15:38:30

How to properly pass data from Vuex to Vue component?

Hello. I am using Vuex in an application. The component has a CHECKBOX on which v-bind hangs. On Watch, I follow the changes in the date that are tied to this checkbox and send it to the STATE. When moving to the next component, the data in the STATE is, of course, saved, and the date in the previous component is erased. So I want that when returning to the previous component, the data in DATES would still be there and those CHECKBOXES that were previously selected were selected. I'm trying to get data from STATE on CREATED and add it to DATE, and it works, but it gives errors. Tell me what is the problem please

<template>
    <div>
        <template v-for="filter in size">
            <input type="checkbox"
                   class="filter-item__checkbox"
                   :name="filter"
                   :value="filter"
                   v-model="selectedSize"
                   id="size"
            >
            <label for="size">
                {{filter}}
            </label>

        </template>
    </div>
</template>

<script>
    import {mapGetters} from 'vuex'

    export default {
        name: "AppSizeFilter",
        data () {
          return {
              selectedSize: []
          }
        },

        computed: {
            ...mapGetters([
                "filtersSize"
            ]),
            size() {
              return this.filtersSize.values
            },
            selectedFilters() {
                return this.$store.state.selected.filters
            },

        },
        watch: {
            selectedSize(size) {
                this.$store.dispatch('updateSize', size);
            }
        },
        created() {
            if(this.selectedSize.length === 0) {
                this.selectedSize = [...this.selectedFilters.selectedSize]
            }
        }
    }
</script>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nvdfxx, 2019-03-25
@nvdfxx

<keep-alive>will probably help you

D
dest86, 2019-03-25
@dest86

https://stackoverflow.com/a/42687186

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question