尝试使用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}
]
}
}
答案 0 :(得分:0)
您需要创建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')