如何在Vue setup()方法中访问Vuex存储?

问题描述 投票:0回答:1

使用 setup() 方法时如何访问 Vue 中的

Vuex
存储?

我无法再访问常规的

this.$store
变量。

javascript vue.js vuex store
1个回答
13
投票

根据组合 API 文档,您必须使用以下语法:

import { computed } from 'vue' // If not already imported
import { useStore } from 'vuex'

export default {
  setup () {
    const store = useStore()

    return {
      // access a state in computed function
      count: computed(() => store.state.count),

      // access a getter in computed function
      double: computed(() => store.getters.double)
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.