Vuejs2 有2个组件的vuex未更新

Vuejs2 有2个组件的vuex未更新,vuejs2,vuex,Vuejs2,Vuex,有人能帮我解决这个问题吗 我有2个vue组件1。有一个按钮(Header.vue) 二,。我想隐藏/显示的侧边栏是否取决于值 标题看起来像这样 <template> <nav class="navbar" role="navigation" aria-label="main navigation"> <div class="navbar-brand"> <a role="button" class="nav

有人能帮我解决这个问题吗

我有2个vue组件1。有一个按钮(Header.vue)

二,。我想隐藏/显示的侧边栏是否取决于值

标题看起来像这样

<template>
    <nav class="navbar" role="navigation" aria-label="main navigation">
        <div class="navbar-brand">
            <a role="button" class="navbar-burger is-pulled-left" aria-label="menu" aria-expanded="false"
               @click='getToggleSidebarMobile'>
                <span aria-hidden="true"></span>
                <span aria-hidden="true"></span>
                <span aria-hidden="true"></span>
            </a>
            <a class="navbar-item " href="/">
                <img :src="'/../images/logo.png'">
            </a>


        </div>

        <div class="navbar-end is-pulled-right">
            <div class="navbar-item">

            </div>
        </div>
    </nav>
</template>

<script>
    import {store} from '../store';

    export default {
        data() {
            return {
                    hideSidebarMobile: store.state.hideSidebarMobile
            }
        },
        methods: {
            getToggleSidebarMobile(){
                this.hideSidebarMobile = !this.hideSidebarMobile;
                store.dispatch('getToggleSidebarMobile', this.hideSidebarMobile);
            }
        }


    }
</script>
我还尝试在模板v-bind:class=“{'is-hidden-touch':sidebar.hiddesidebarmobile}”中使用,但在那里也没有运气,因为我可以看到值被更新了,但我无法添加/删除该类。我哪里出错了


更新。。。忘记上传store

侧栏模板中的
store.state.hidesibermobile
引用不起作用。(除非您在侧边栏的Vue实例上设置了与Vuex存储相同的
store
属性,我假设您没有设置该属性。)

如果已正确注册Vuex模块:

const store = new Vuex.Store({ ... }); // your store config
Vue.use(Vuex); // registering the plugin
new Vue({ // your root Vue instance
  el: '#app',
  store: store, // passing the `store` so components can reference this.$store
  ...
});
然后,您应该能够通过
this.$store
在侧边栏组件中引用存储。这也意味着不需要将
store
导入到每个需要引用它的文件中

因此,在您的模板中:

v-bind:class="{'is-hidden-touch' : $store.state.hideSidebarMobile}"

侧边栏模板中的
store.state.hidesibermobile
引用不起作用。(除非您在侧边栏的Vue实例上设置了与Vuex存储相同的
store
属性,我假设您没有设置该属性。)

如果已正确注册Vuex模块:

const store = new Vuex.Store({ ... }); // your store config
Vue.use(Vuex); // registering the plugin
new Vue({ // your root Vue instance
  el: '#app',
  store: store, // passing the `store` so components can reference this.$store
  ...
});
然后,您应该能够通过
this.$store
在侧边栏组件中引用存储。这也意味着不需要将
store
导入到每个需要引用它的文件中

因此,在您的模板中:

v-bind:class="{'is-hidden-touch' : $store.state.hideSidebarMobile}"

因为这是一个laravel项目,所以我确实从“/store”和store:store导入了{store},它成功了。。。感谢这是一个laravel项目,我确实从“/store”和store:store导入了{store},它成功了。。。谢谢