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 Vuex状态未使用变异更新_Javascript_Vue.js_State_Vuex - Fatal编程技术网

Javascript Vuex状态未使用变异更新

Javascript Vuex状态未使用变异更新,javascript,vue.js,state,vuex,Javascript,Vue.js,State,Vuex,我试图将状态的值从false改为true,但它似乎不起作用。下面我将添加我的存储、只获取状态的组件和应该更新状态的组件 //贮藏 //获取状态的组件 <template> <div class="home"> <h1>{{newComment}}</h1> <SelectedVideo v-bind:user="user" v-bind:video="videos[0]"/> </d

我试图将状态的值从false改为true,但它似乎不起作用。下面我将添加我的存储、只获取状态的组件和应该更新状态的组件

//贮藏

//获取状态的组件

<template>
    <div class="home">
        <h1>{{newComment}}</h1>
        <SelectedVideo v-bind:user="user" v-bind:video="videos[0]"/>
    </div>
</template>

<script>
    import axios from 'axios';
    import SelectedVideo from '../components/SelectedVideo.component';
    axios.defaults.withCredentials = true;

    export default {
        name: 'Home',
        components: {
            SelectedVideo
        },
        data() {
            return {
                videos: [],
                user: null
            }
        },
        computed: {
            newComment() {
                return this.$store.state.newComment
            }
        },
methods: {
                updateComments() {
                    this.$store.dispatch('updateComments', true);
                }
            },
            async createComment(comment, video, user) {
                try{
                    const res = await axios({
                        method: 'POST',
                        url: 'http://localhost:8000/api/v1/comments/',
                        data: {
                            comment,
                            video,
                            user
                        }
                    });
                    if (res.data.status === 'success') {
                        console.log(res);
                        // location.reload(true);
                        this.updateComments();
                    }
                } catch(err) {

我正在成功获取状态,但当我尝试调用函数进行更新时,更新似乎对状态没有影响。

首先为使用状态数据制作一个getter

getters:{
   get_newComment: state => state.newComment
}
然后将其实现到计算属性中

computed: {
            newComment() {
                return this.$store.getters.get_newComment
            }
        }

然后使用“提交”来提交一些更改,而不是“分派”。分派用于调用操作。

首先制作一个用于使用状态数据的getter

getters:{
   get_newComment: state => state.newComment
}
然后将其实现到计算属性中

computed: {
            newComment() {
                return this.$store.getters.get_newComment
            }
        }

然后使用“提交”来提交一些更改,而不是“分派”。调度用于调用操作。

提交
调度
this.$store.commit('updateComments',true)我也试过了,但没有效果。正确的语法应该是
this.$store.commit('updateComments',true)。更改后,是否可以添加
console.log(this.$store.state.newComment)
以查看存储是否已更新,现在我看不到您的代码中有其他错误
commit
not
dispatch
this.$store.commit('updateComments',true)我也试过了,但没有效果。正确的语法应该是
this.$store.commit('updateComments',true)。更改后,是否可以添加
console.log(this.$store.state.newComment)
以查看存储是否已更新,现在我看不到您的code@metmet-pekcan,你的getter实际上对数据没有任何作用。因此,您可以直接访问该值。计算属性也是如此。您可以查看vuex的
mapState
将状态值映射到组件的
data()
@Ronald是的,这里的问题不在于此,但使用状态值的最佳实践是使用getter。我刚刚解决了这个问题。(当然,mapState会进入计算状态,而不是数据,oops)@metmet pekcan,你的getter实际上不会对数据做任何事情。因此,您可以直接访问该值。计算属性也是如此。您可以查看vuex的
mapState
将状态值映射到组件的
data()
@Ronald是的,这里的问题不在于此,但使用状态值的最佳实践是使用getter。我刚刚解决了这个问题。(当然,mapState会进入计算状态,而不是数据,oops)