vue3.0中使用store

6,615 阅读1分钟

vuex中提供useStore 在setup中可以使用

useStore ===vue2.0中的this.$store

import { useStore } from 'vuex'
export default {
  setup () {
    // 在setup中获取store
    const store = useStore()
    //根据computed获取store的state
    const catagryList = computed(() => {
      return store.state.catagory.catagryList
    })
    onMounted(() => {
      store.dispatch('catagory/getCatagroy')
    })
    return { catagryList }
  }
}
</script>

注意: userStore() 调用生效 不然报错