搜尋

首頁  >  問答  >  主體

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} 從 'vue' import App from './App.vue' import router from './router' import { createStore } 從 '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 } 從 '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>
P粉321584263P粉321584263546 天前434

全部回覆(1)我來回復

  • P粉322918729

    P粉3229187292023-08-26 00:23:03

    嘗試使用:

    createApp(App)
      .use(router)
      .use(store)
      .mount('#app')

    回覆
    0
  • 取消回覆