最初是作为一个实验为 Vue重新设计状态管理,让它用起来像组合式API
npm install pinia
安装piniaimport { createPinia } from "pinia";
export default createPinia();
import { createApp } from "vue";
import App from "./App.vue";
import pinia from "./store";
const app = createApp(App);
app.use(pinia);
app.mount("#app");
import { defineStore } from "pinia";
//defineStore创建store,可以创建多个store
//第一个参数是用于命名store的
//第二个参数是store的具体内容
const useCounter = defineStore("counter", {
state: () => ({
count: 199,
}),
});
//返回值是一个函数,将函数暴露出去
export default useCounter;
<template>
{{ counterStore.count }}
template>
<script setup>
//引入暴露的store函数
import useCounter from "./store/counter.js";
const counterStore = useCounter();
script>
<style scoped>style>
在上面的例子中有讲过store的定义
import { defineStore } from "pinia";
//defineStore创建store,可以创建多个store
//第一个参数是用于命名store的
//第二个参数是store的具体内容
const useCounter = defineStore("counter", {
state: () => ({
count: 199,
}),
});
//返回值是一个函数,将函数暴露出去
export default useCounter;
import { defineStore } from "pinia";
//defineStore创建store,可以创建多个store
//第一个参数是用于命名store的
//第二个参数是store的具体内容
const useCounter = defineStore("counter", {
state: () => ({
count: 199,
}),
});
//返回值是一个函数,将函数暴露出去
export default useCounter;
//引入暴露的store函数
import useCounter from "./store/counter.js";
const counterStore = useCounter();
//读取
counterStore.count
//修改
counterStore.count++
//引入暴露的store函数
import useCounter from "./store/counter.js";
const counterStore = useCounter();
counterStore.$reset()//会将counter恢复到初始定义的状态
//引入暴露的store函数
import useCounter from "./store/counter.js";
const counterStore = useCounter();
counterStore.$patch({
name:"zhangcheng",
age:"18"
})
//引入暴露的store函数
import useCounter from "./store/counter.js";
const counterStore = useCounter();
counterStore.$state = {
name:"zhangcheng",
age:"18"
}
import { defineStore } from "pinia";
const useCounter = defineStore("counter", {
state: () => ({
count: 199,
}),
getters:{
doubleCount(state){
//通过传参的方式进行访问
return state.count
},
doubleCountAddOne(){
//可以通过this进行访问
return this.count*2+1
}
}
});
//返回值是一个函数,将函数暴露出去
export default useCounter;
//使用方法同state,先引入暴露的函数,执行,通过.的方式进行访问
import { defineStore } from "pinia";
const useCounter = defineStore("counter", {
state: () => ({
count: 199,
}),
getters:{
doubleCount(state){
//通过传参的方式进行访问
return state.count
},
doubleCountAddOne(){
//可以通过this访问getters中的变量
return this.doubleCount+1
}
}
});
//返回值是一个函数,将函数暴露出去
export default useCounter;
import { defineStore } from "pinia";
//引入其他的store
import userInfo from "./store/user.js";
const useCounter = defineStore("counter", {
state: () => ({
count: 199,
}),
getters:{
getMessage(){
//实例化而后使用
const userStore = userInfo()
return userInfo.name + count
}
}
});
//返回值是一个函数,将函数暴露出去
export default useCounter;
import { defineStore } from "pinia";
//引入其他的store
import userInfo from "./store/user.js";
const useCounter = defineStore("counter", {
state: () => ({
count: 199,
}),
actions:{
countAdd(num){
//可以传入参数
//直接通过this进行访问state中的状态
this.count += num
}
}
});
//具体使用
//引入暴露的store函数
import useCounter from "./store/counter.js";
const counterStore = useCounter();
//直接调用方法即可
counterStore.countAdd(10)
import { defineStore } from "pinia";
//引入其他的store
import userInfo from "./store/user.js";
const useCounter = defineStore("counter", {
actions:{
async getData(){
const res = await fetch(xxxx)
const data = await res.json()
//改变state状态
this.list = data
//返回data
retrun data
}
}
});