【问题标题】:Vuex state getter not working inside the vue 3 composition apiVuex 状态获取器在 vue 3 组合 API 中不起作用
【发布时间】:2021-06-20 20:59:53
【问题描述】:

尝试实现新的 Vue 组合 API 我遇到了一个问题,我的 vuex 状态值不起作用,它将返回 $store 未定义错误。当我检查我的语言状态是否已设置时,它已设置为存在数据。我找不到任何关于此的好信息,但我确实找到了一些帮助插件,但我不想为每个问题都使用插件,所以有没有办法做到这一点?

我正在使用 vue 3 和 vuex 4

export default {
    setup (props, context) {
        console.log(context.root.$store.getters['i18n/language'])//not working
        // more logic here
    }
}

【问题讨论】:

    标签: vue.js vuex


    【解决方案1】:

    您应该使用名为useStore 的可组合函数来获取商店实例:

    import {useStore} from 'vuex'
    export default {
        setup (props, context) {
            const store=useStore()
            console.log(store.getters['i18n/language'])
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 1970-01-01
      • 2021-04-22
      • 2021-06-24
      • 2023-03-04
      • 2021-02-10
      • 2021-10-28
      • 2021-06-22
      • 2021-10-04
      相关资源
      最近更新 更多