1,引入vuex
import { createStore } from "vuex";
2,创建store
const store = createStore({
state(){
return {
count: 1,
}
}
mutations: {
add(state){
state.count++
}
}
});
3,main.js 中引入store
createApp(App) .use(store)
4,在组件中使用store
<p @click="add">{{ count }}</p> ... import { useStore } from 'vuex'; import { toRefs } from 'vue'; ... setup() { const store = useStore(); return{ ...toRefs(store.state), add() { store.acmmit('add'); } } }