setup函数使用vuex
问题: Vue3里的setup中如何使用vuex
描述: 在Vue2项目中可以使用this.$store获取vuex里的数据和保存全局数据,但是在Vue3的setup函数里,并没有this这个概念,因此如何使用路由方法
解决: 使用代替this的useStore,具体使用如下:
<script>
//引入路由函数
import { useStore } from "vuex";
//使用
setup() {
//使用vuex
const store = useStore();
//正常使用,相当于store代替了this.$store
store.state ...
return {};
}
</script>
问题: Vue3里的模板里展示vuex的数据
<template>
<p>{{ $store.state.counter }}</p>
<button @click="$store.commit('')"></button>
</template>