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

mar*_*use 9 javascript store vue.js vuex

使用方法时如何访问Vue中的Vuexsetup() store ?

我无法再访问常规this.$store变量。

mar*_*use 15

根据组合 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)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 如何在 vue 2.7 中做到这一点(使用组合 API)? (2认同)