使用vuex,更新了stores中的state,为何在视图上没有更新体现呢?

vue小白一枚来求解,代码是这样的
组件.vue

<template>
  <div class="top-ad-section" v-bind:class="{ show: isShowAdStatus }"></div>
</template>

<script>
export default {
  mounted() {
    setTimeout(() => {
      console.log(this.$store.getters.getTopAdStatus, 'before lllll');
      this.$store.dispatch('toggleTopAd');
      console.log(this.$store.getters.getTopAdStatus, 'after');
    }, 200);
  },
  computed: {
    isShowAdStatus() {
      return this.$store.getters.getTopAdStatus;
    }
  }
};
</script>

这段代码要实现的功能是:
在组件加载200ms后改变store中的state的值,使用this.$store.dispatch('toggleTopAd')来改变,这个值可以用this.$store.getters.getTopAdStatus取到,这时候打印出的值是正确的state里的值,可是并没有体现在视图里,不知道是哪里写的有问题了

小白一枚,求大神指点。
以前写react的时候主要用mobx做状态管理,感觉vuex的状态管理至少在形式上是跟redux更接近的?

阅读 3.4k
1 个回答

因为我的vue是用script标签引入的,然后我在store.js里面又用import的方式引入了一遍,后来把store.js里面的这句import Vue from 'vue';删掉就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题