Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么州政府不';在计算机中不更新?_Javascript_Vue.js_Vuex - Fatal编程技术网

Javascript 为什么州政府不';在计算机中不更新?

Javascript 为什么州政府不';在计算机中不更新?,javascript,vue.js,vuex,Javascript,Vue.js,Vuex,我在vuex上挣扎了几个小时。 这是我第一次使用它,尽管我遵循文档,但有些东西不起作用,但我真的无法理解 因此,我有了vuex文档的第一个不起作用的示例。 我有一个count计算值,返回存储状态 它会显示在页面上。 起始值为0,视图显示为0。 但是按下按钮并不会改变显示 如果我以常量将商店直接放入App.js中,那么毫无问题,它可以正常工作 我知道我错过了一些东西 homeStore.js import Vue from 'vue' import Vuex from 'vuex' Vue.us

我在vuex上挣扎了几个小时。 这是我第一次使用它,尽管我遵循文档,但有些东西不起作用,但我真的无法理解

因此,我有了vuex文档的第一个不起作用的示例。 我有一个
count
计算值,返回存储状态

它会显示在页面上。 起始值为0,视图显示为0。 但是按下按钮并不会改变显示

如果我以常量将商店直接放入App.js中,那么毫无问题,它可以正常工作

我知道我错过了一些东西

homeStore.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        count: 0
    },
    mutations: {
        increment (state) {
            state.count++
        },
        decrement (state) {
            state.count--
        }
    }
})
App.js

import store from '../store/homeStore'

new Vue({
    el: '#app',
    store,
    computed: {
        count () {
            return store.state.count
        },
    },
    methods: {
        increment () {
            store.commit('increment')
        },
        decrement () {
            store.commit('decrement')
        }
    }
})

如果尝试
console.log(store)
,它将返回未定义。
访问存储的正确方法是使用
this.$store
(应该是一个组件)

通过向根实例提供存储选项,存储将被注入到根的所有子组件中,并作为this.$store在这些子组件上可用

因此,您的更新代码应该是:

methods: {
    increment () {
        this.$store.commit('increment')
    },
    decrement () {
        this.$store.commit('decrement')
    }
}

利用Vuex存储操作

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
    state: { count: 0 },
    mutations: {
        increment (state) {
            state.count++ 
        },
       decrement (state) {
           state.count-- 
       }
    },
    actions: {
        increase ({ commit }) {
            commit('increment')
        },
        decrease ({ commit }) {
            commit('decrement')
        }
    }
 })

使用此访问操作方法。$store.dispatch('increment')以增加计数,反之亦然。
this。$store.commit('increment')
。否,因为它是应用程序方法,而不是组件。提示:在chrome上安装Vue插件,并检查是否触发了突变。@iRohitBhatia我有。我将console.log放入函数中,它会触发。我甚至还添加了一个按钮,当我点击它时,它会显示store.state.count,并且可以正常工作。唯一不起作用的是视图中计算函数的自动更新。我已经看到了。它需要组件,我不在其中。我复制了文档的入门演示,但是我在商店中使用了一个文件。我不确定你是否可以通过这种方式访问它。但您可以像演示一样设置: