npm i pinia
store/index.js
文件import { defineStore } from "pinia"// defineStore(当前 store 的 Id, {配置项})
export const countStore = defineStore("count", {// 状态state: () => {return {count: 1}},// 计算属性getters: {// 这里的计算属性使用时,为一个属性而不是方法increaseNum(store) {return store.count * 10}},// 方法actions: {addCount(value) {this.count += value}}
})
main.js
中引入// 这是 Vue3 的引入方式,Vue2 不太一样
import { createApp } from "vue";
import App from "./App.vue";
import { createPinia } from "pinia";const app = createApp(App);
app.use(createPinia());
app.mount("#app");
这样就可以在任意位置引入 store 了