【问题标题】:computed property 'name' was assigned but it has not setter (without v-model)已分配计算属性“名称”,但没有设置器(没有 v-model)
【发布时间】:2018-12-14 22:24:44
【问题描述】:

我试图通过添加类nav-selected 使导航在单击时处于活动状态。我见过其他类似的问题,但它们与v-model 有关,因此它们没有帮助我。

我需要确保它已添加到商店中,以便即使在刷新后我也始终可以看到哪个页面处于活动状态。但是我收到以下错误:

Navigation.vue:

    <template>
    <v-container>
        <v-layout align-center>
            <!-- Logo -->
            <v-flex sm2>
                <img src="http://beam.space/img/icon.png" height="30px">
            </v-flex>
            <!-- Navigation -->
            <v-flex sm8>
                <v-layout wrap justify-center>
                    <v-flex sm2>
                        <router-link to="/myspaces">
                            <h2 @click="setActiveNav(0)" :class="{'nav-selected': activeNavigation === 0}" class="nav-text">My Spaces</h2>
                        </router-link>

                    </v-flex>

                    <v-flex sm2>
                        <router-link to="/inspirations">
                            <h2 @click="setActiveNav(1)" :class="{'nav-selected': activeNavigation === 1}" class="nav-text">Inspirations</h2>
                        </router-link>
                    </v-flex>
                </v-layout>
            </v-flex>
            <v-flex sm2>
                <p>profile</p>
            </v-flex>
        </v-layout>
    </v-container>
</template>

<script>
    import { mapState } from 'vuex';

    export default {
        name: "navigation",
        computed: {
            ...mapState([
                'activeNavigation'
            ])
        },
        methods: {
            setActiveNav(activeNav) {
                this.activeNavigation = activeNav;
                this.store.commit('setActiveNavigation', this.activeNavigation);
            }
        }
    }
</script>

<style scoped>

</style>

Store.js:

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

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        activeNavigation: 0
    },
getters: {

    },
    mutations: {
        // Set Active navigation on click.
        setActiveNavigation(state, id) {
            state.activeNavigation = id;
        }

    },

    // actions zijn a sync
    actions: {
    }
});

【问题讨论】:

    标签: vue.js vuejs2 vue-component vuex


    【解决方案1】:

    mapState 仅创建 getter 属性。您不能分配给这些属性。

    反正不用赋值给activeNavigation,提交会自动更新activeNavigation的映射值。

    改变这个:

    setActiveNav(activeNav) {
        this.activeNavigation = activeNav;  // <-- Not allowed
        this.store.commit('setActiveNavigation', this.activeNavigation);
    }
    

    到这里:

    setActiveNav(activeNav) {
        this.$store.commit('setActiveNavigation', activeNav);
    }
    

    【讨论】:

    • 有道理!但是现在我收到以下错误:Uncaught TypeError: Cannot read property 'commit' of undefinedactiveNav 不是未定义的,那是什么?
    • store 应该是 $store
    • 完美,但是..当我重新加载页面时,它似乎没有记住 activeNavigation。它重新加载并重置activeNavigation。我是否对商店的运作方式感到困惑?
    • Vuex 状态不会在页面重新加载后持续存在。这里有solutions,但这是另一个问题,我不会在这里详细介绍。
    猜你喜欢
    • 2018-02-16
    • 2019-05-21
    • 2020-07-09
    • 2020-10-04
    • 2018-11-02
    • 2018-09-26
    • 2021-03-17
    • 2020-11-07
    • 2020-07-18
    相关资源
    最近更新 更多