简体   繁体   English

vuex状态和吸气剂已更新,但计算值不起作用

[英]vuex state and getters are updated, but computed value doesn't react

I'm currently trying to implement a simple theme-switching function in which the active theme is saved in a vuex store state. 我目前正在尝试实现一个简单的主题切换功能,其中活动主题以vuex存储状态保存。 Upon pressing a button the theme is supposed to switch. 按下按钮后,主题应该切换。

Here is a video with vue-devtools, demonstrating the problem 这是带有vue-devtools的视频,演示了问题所在

As you can see in the video the data is successfully changed in the state and the getter returns the correct value, however the computed value of my component doesn't react to the change. 正如您在视频中看到的那样,状态中的数据已成功更改,并且getter返回正确的值,但是我组件的计算值对更改没有反应。

/src/main.js /src/main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import Vuex from 'vuex'
import Icon from 'vue-awesome/components/Icon'

Vue.use(Vuex)
Vue.component('icon', Icon)

new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

/src/App.vue /src/App.vue

<template>
  <div id="app"
       :class="themeName">
    <div id="themeSwitch"
         v-on:click="switchTheme">
      <icon name="lightbulb"
            :class="themeName"></icon>
    </div>
    <router-view/>
  </div>
</template>

<script>
import "vue-awesome/icons/lightbulb"

import { mapState, mapGetters } from "vuex"

var app = {
  name: "App",
  beforeCreate() {
    this.$store.dispatch("LOAD_THEME")
  },
  computed: {
    themeName() {
      return this.$store.getters.GET_THEME
    }
  },
  methods: {
    switchTheme: function(event) {
      this.$store.dispatch("SWITCH_THEME")
    }
  }
};

export default app;
</script>

/src/store/index.js /src/store/index.js

import Vue from "vue/dist/vue.common.js"
import Vuex from "vuex/dist/vuex.js"
import * as Cookies from "tiny-cookie"

Vue.use(Vuex);

const themes = ["dark", "light"];
const store = new Vuex.Store({
  state: {
    themeName: ''
  },
  actions: {
    LOAD_THEME({ commit, state }) {
      if (state.themeName.length > 0) return

      var themeId = Cookies.getCookie("themeId")
      if (!themeId) commit("SET_COOKIE", themeId = 1)

      commit("SET_THEME", themeId)
    },
    SWITCH_THEME({ commit, state }){
      var id = themes.indexOf(state.themeName) < 1 ? 1 : 0
      commit("SET_THEME", id)
      commit("SET_COOKIE", id)
    }
  },
  getters: {
    GET_THEME: state => {
      return state.themeName
    }
  },
  mutations: {
    SET_COOKIE: (state, id) => {
      Cookies.setCookie("themeId", id, { expires: "1M" })
    },
    SET_THEME: (state, id) => {
      state.themeName = themes[id]
    }
  }
});

export default store;

I tried a few different approaches for the computed property, which I found all over the internet. 我尝试了几种用于计算属性的方法,这些方法在Internet上都可以找到。 But none of them made any difference. 但是它们都没有任何不同。

computed: mapState({
    themeName: state => state.themeName
  })

computed: {
  ...mapGetters({
    themeName: 'GET_THEME'
  })
}

If I use data instead of computed and I manually set the string it works, but that defeats the purpose of the state if I have to manually set every local variable in every component. 如果我使用数据而不是计算数据,而是手动设置字符串,那么它可以工作,但是如果我必须手动设置每个组件中的每个局部变量,那将破坏状态的作用。

Any help would be appreciated. 任何帮助,将不胜感激。

Looks like you are using two different instances of Vue. 看起来您正在使用Vue的两个不同实例。 In main.js you are importing vue but in src/store/index.js you are importing vue/dist/vue.common.js and telling each one to use Vuex. main.js您要导入vue但在src/store/index.js您要导入vue/dist/vue.common.js并告诉每个人使用Vuex。 Try using vue for both imports. 尝试对两个导入都使用vue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM