Pinia 是 Vue 的存储库,它允许您跨组件/页面共享状态。
npm install pinia
import { createApp } from "vue";
import App from "./App.vue";
import { createPinia } from "pinia";
const app = createApp(App);
app.use(createPinia());
app.mount("#app");
import { defineStore } from "pinia";
export const useStore = defineStore("main", {
state: () => {
return {
name: "yuchunyi",
age: 18
};
},
getters: {
ageDouble(state) {
return state.age * 2;
},
doubleCountPlusOne() {
// 可以通过this访问到useStore
return this.doubleCount + 1;
}
},
actions: {
increment() {
this.age++;
},
//异步操作
async registerUser(login, password) {
const res = await login(login, password);
return res.data;
}
}
});
<template>
<div>{{ store.name }}</div>
<div>{{ store.age }}</div>
<div>{{ name }}</div>
<div>{{ age }}</div>
</template>
<script lang="ts" setup>
import { useStore } from "@/store/index";
import { storeToRefs } from "pinia";
// 普通使用,需要store.name、store.age
const store = useStore();
// 使用解构,直接用name age,如果不用storeToRefs,就没响应式
const { name, age } = storeToRefs(useStore());
onMounted(() => {
store.increment();
});
</script>
const handleClick = () => {
store.name = "lili";
store.age++;
};
const handleClickPatch = () => {
store.$patch({
name:"lili"
age: store.age ++;
});
};
const handleClickMethod = () => {
store.$patch((state) => {
state.age++;
state.name = "lili";
});
};
actions:{
changeState(){
this.age++
this.name='lili'
}
}
调用
//在 template
<button @click="store.changeState">按钮</button>
//在 script
store.changeState()
与计算属性类似,有缓存效果,依赖项的变化重新触发方法 getters 里面的方法相对于 state 里面变量
getters: {
getAge(state) {
return state.age + '岁'
}
}
<div>{{ store.getAge }}</div>
<!-- const { getAge } = storeToRefs(useStore()); -->
<div>{{ getAge }}</div>
actions: {
increment() {
this.age++;
},
//异步操作
async registerUser(login, password) {
const res = await login(login, password);
return res.data;
}
}
import { useStore } from "@/store/index";
const store = useStore();
onMounted(() => {
store.increment();
const data = store.registerUser();
});
pinia 在外部 ts 文件中使用时。要创建 pinia 实例,然后传入 useStore 方法中,否则会报错。
import { createPinia } from "pinia";
import { useMainStore } from "@/store/index";
const store = useMainStore(createPinia());