无法将 vue 存储绑定到状态



尝试使用 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}
]
}
}

您需要创建一个新的 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')

最新更新