【问题标题】:Vuex4 - Cannot read properties of undefined (reading 'state')Vuex4 - 无法读取未定义的属性(读取“状态”)
【发布时间】:2021-11-28 18:17:40
【问题描述】:

我正在使用 Vue3 和 Vuex4,我不断收到错误:

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)

我正在尝试将“hello”设置为消息变量,以便我可以在整个应用程序中使用它,这是我的 main.js 文件:

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')

这是我的 App.vue 文件,我正在尝试使用计算函数通过组合 API 接收它。

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{ }
  }
}

【问题讨论】:

    标签: javascript vue.js vuex vuejs3


    【解决方案1】:

    尝试:

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

    【讨论】:

      猜你喜欢
      • 2019-06-08
      • 2021-11-18
      • 2019-07-25
      • 2020-11-05
      • 2021-07-07
      • 2020-11-10
      • 1970-01-01
      相关资源
      最近更新 更多