目录结构
scr
|--store
|--user
创建
import { defineStore } from "pinia";
export const useStore = defineStore({
id: "user_test",
state: () => ({
count: 2,
}),
getters: {
countPow2(state) {
return state.count ** 2;
},
},
actions: {
increment() {
this.count++;
},
},
});
用法
<script setup >
import { useStore } from "@/store/store/user";
let store = useStore();
console.log(store.count)
console.log(store.countPow2)
console.log(store.increment())
</script>
main.js中注册
import { createApp } from "vue";
import App from "./App.vue";
import { createPinia } from "pinia";
let app = createApp(App);
app.config.productionTip = false;
app.use(createPinia());
app.mount("#app");