Vuex4 - 无法访问未定义的属性(访问 'state')
<p>我正在使用<code>Vue3</code>和<code>Vuex4</code>,但是一直出现以下错误:</p>
<pre class="brush:php;toolbar:false;">Uncaught TypeError: Cannot read properties of undefined (reading 'state')
at ReactiveEffect.eval [as fn] (App.vue?3dfd:36)
at ReactiveEffect.run (reactivity.esm-bundler.js?a1e9:160)
at ComputedRefImpl.get value [as value] (reactivity.esm-bundler.js?a1e9:1087)
at setup (App.vue?3dfd:37)
at callWithErrorHandling (runtime-core.esm-bundler.js?5c40:6656)
at setupStatefulComponent (runtime-core.esm-bundler.js?5c40:6272)
at setupComponent (runtime-core.esm-bundler.js?5c40:6228)
at mountComponent (runtime-core.esm-bundler.js?5c40:4081)
at processComponent (runtime-core.esm-bundler.js?5c40:4056)
at patch (runtime-core.esm-bundler.js?5c40:3651)</pre>
<p>我想将“hello”设置为message变量,以便在整个应用程序中使用,这是我的<code>main.js</code>文件:</p>
<pre class="brush:php;toolbar:false;">import { createApp} from 'vue'
import App from './App.vue'
import router from './router'
import { createStore } from 'vuex';
const store = createStore({
state(){
return{
message: 'hello'
}
}
})
createApp(App).use(router, store).mount('#app')</pre>
<p>这是我的App.vue文件,我正在尝试使用计算函数和组合式API接收它。</p>
<pre class="brush:php;toolbar:false;">import { ref, computed } from 'vue'
import { useStore } from 'vuex'
export default {
name: 'App',
setup(){
const store = useStore();
const message = computed(() => store.state.message);
console.log(message.value);
return{ }
}
}</pre></p>