【问题标题】:Data not changing when store state changes存储状态更改时数据不更改
【发布时间】:2021-01-23 10:33:52
【问题描述】:

我在一个组件中有一些数据设置为存储状态,如下所示:

data() {
    return {
      days: [],
      selectedDate: this.selectedDateComputed,
      dayJs: this.$store.state.dayJS,
    };
  },

但是当商店状态更新时 它不会更新组件中的 dayJs 状态。

我尝试将其设为计算属性,使用 selectedDate 数据如下:

selectedDateComputed() {
    return this.$store.state.dayJS;
  },

但 selectedDate 也不更新。

【问题讨论】:

    标签: vue.js vuex nuxt.js


    【解决方案1】:

    如果要检测 store 状态的变化,需要使用 Vuex 提供的mapState helper 函数。看看the docs

    import { mapState } from "vuex";
    
    export default {
      // ...
      computed: {
        ...mapState(["dayJS"])
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2020-02-28
      相关资源
      最近更新 更多