简体   繁体   English

VueJS-Vuex状态更新时组件未更新

[英]VueJS - Components not updating when Vuex state updated

I have a vuex store with an item called "columnChoice" inside of the state. 我在州内有一家名为“ columnChoice”的项目的vuex商店。 I have a component that's updating it in a text box, and a computed property that updates it and verifies that it's a positive integer. 我有一个在文本框中更新它的组件,以及一个更新它并验证它是正整数的计算属性。 However, the component's computed properties aren't updating (according to the dev tools), even though the vuex store definitely is (also according to the dev tools). 但是,组件的计算属性不会更新(根据dev工具),即使vuex存储肯定是(根据dev工具)也是如此。

Here's the code, I've removed the other methods/values that aren't relevant, but if it seems like something's missing let me know. 这是代码,我删除了其他不相关的方法/值,但是如果似乎缺少某些内容,请告诉我。

I've tried changing the computed setup from the "...mapState([])" one to this one, and I also tried the v-model setup with seperate set and get functions. 我尝试将计算的设置从“ ... mapState([])”更改为这一设置,并且还尝试了使用单独的set和get函数进行v模型设置。

the Vuex store index.js : Vuex商店index.js

import Vuex from 'vuex'

export default new Vuex.Store({
  state: {
    columnChoice: 1,
    processingStatus: 0,
    columnError: 0
  },
  mutations : {
    resetVars(state) {
      state.processingStatus = 0
      state.columnError = 0
      state.columnChoice = 1
    },

    setProcessingStatus(state, v) {
      state.processingStatus = v
    },

    columnError(state) {
      state.columnError = 1
    },
    columnGood(state) {
      state.columnError = 0
    },
    columnSet(state, v) {
      state.columnChoice = v
    }
  }
})

The component: 组件:

<template>
  <div class="row p-2">
    <div class="col-2">Column in reference file</div>
    <div class="col-10"><input type=text size=3 :value="columnChoice" @change="verifyColumn" id="columnChoice"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'ColumnChoice',
  computed: {
    ...mapState([
      'columnChoice'
    ]),
  },
  methods: {
    verifyColumn(e) {
      if (isNaN(e.target.value) || e.target.value < 1) {
        this.$store.commit('columnError')
        this.$store.commit('columnSet', e.target.value)
      } else {
        this.$store.commit('columnGood')
        this.$store.commit('columnSet', e.target.value)
      }
    },
  }
}
</script>

Additionally, after changing the value to 5 in the text field and selecting this component in the dev tools (assigning it to $vm0), I get the following, showing that the state is indeed updated and accessible through the component, but that the computed property just isn't updating: 此外,在文本字段中将值更改为5并在开发工具中选择此组件(将其分配给$ vm0)之后,我得到以下信息,该状态确实已更新且可通过该组件访问,但计算得到的状态属性只是没有更新:

$vm0.$store.state.columnChoice
> "5"
$vm0._computedWatchers.columnChoice.value
> "1"

Alright, I figured it out. 好吧,我知道了。 Turns out in my index.html file I had been fetching a vue instance from a CDN in addition to using the one imported from NPM 结果是在我的index.html文件中,除了使用从NPM导入的那个实例外,我还从CDN中获取了一个vue实例。

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

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