代码之家  ›  专栏  ›  技术社区  ›  DrRoach

无法将Vue存储绑定到状态

  •  0
  • DrRoach  · 技术社区  · 8 年前

    尝试使用Vuex时,出现以下错误: [Vue warn]: Error in render: "TypeError: Cannot read property 'state' of undefined"

    这是我的代码:

    src/main.js

    import Vue from 'vue';
    import Vuex from 'vuex';
    Vue.use(Vuex);
    
    import store from './Store';
    
    new Vue({
        render: h => h(App),
        store: Vuex.Store(store)
    }).$mount('#app')
    

    src/App.vue

    <template>
    <div>
        <h1>Todos</h1>
        <p v-for="todo in todos">{{ todo.body }}</p>
    </div>
    </template>
    
    <script>
    import { mapState } from 'vuex'
    
    export default {
        computed: {
            todos() {
                return this.$store.state.todos;
            }
        }
    }
    </script>
    
    <style>
    </style>
    

    src/Store.js

    export default {
        state: {
            todos: [
                {body: "Go to store", done: false},
                {body: "Go to work", done: false},
                {body: "Go to bed", done: false},
                {body: "Go to bank", done: false},
                {body: "Go to school", done: false}
            ]
        }
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   GTHell    8 年前

    您需要创建vuex.store(store)的新实例。

    修改此块

    new Vue({
        render: h => h(App),
        store: Vuex.Store(store)
    }).$mount('#app')
    

    new Vue({
        render: h => h(App),
        store: new Vuex.Store(store)
    }).$mount('#app')
    
    推荐文章