Vuex Store - 变量仅在 Store 中更新,而不是全局更新

问题描述 投票:0回答:0

我在组件中更改存储值后,我正在尝试访问它。 首先,我从列表中选择一组数字,然后在按下按钮后将其存储在商店中。当我尝试从商店访问价值时,它仍然是空的。

我的 Vue 组件:

<template>
   <MultiSelect
        class="groupselect"
        v-model="localSelectedNumbers"
        :options="options"
        optionLabel="id"
        placeholder="Wähle Geräte"


    />
  <v-btn @click="refresh()">Click</v-btn>
  <p>{{selectedNumbers}}</p>
</template>
<script>
import MultiSelect from "primevue/multiselect";
import {mapMutations, mapState} from "vuex";

export default {
  name: "Gerateauswahl",
  components: {MultiSelect},
  computed: {
    ...mapState([
        'mode',
        'selectedNumbers',
        'options'
    ])
  },
  data() {
    return {
      localSelectedNumbers: [],
      show: []
    }
  },
  watch: {
    selectedNumbers(newValue, oldValue) {
      this.show = newValue
    }
  },
  methods: {
    ...mapMutations([
        'setRows'
    ]),
    refresh() {
      this.setRows(JSON.parse(JSON.stringify(this.localSelectedNumbers)))
      //console.log(this.selectedNumbers)
    }
  }
}
</script>

我的店铺:

import {createStore} from "vuex";

const store = createStore({
    state() {
        return {
            options : [
                {id:1}, {id:2}, {id:3}
            ],
            rows: [],
            mode: false,
            selectedNumbers: []
        }
    },
mutations: {
        setRows(state, payload) {
            console.log(state.rows)
            state.rows = payload
            console.log(state.rows)
        },
}
export default store;

我试过观察者,manuel refresh。 Store 中的 console.log 输出:(尝试使用数字 1 和 2)

<target>: Array [ {…}, {…} ]
0: Object { id: 1 }
1: Object { id: 2 }
length: 2​​
<prototype>: Array []
<handler>: Object { get: get(target, key, receiver), set: set(target, key, value, receiver), deleteProperty: deleteProperty(target, key), … }

但是 Vue 组件中的 selectedNumbers 变量以及从组件访问时的 this.$store.state.selectedNumbers 保持为空。

javascript vue.js vuex store
© www.soinside.com 2019 - 2024. All rights reserved.