一、store.js中创建actions
import {
defineStore } from "pinia";
export const useStore = defineStore('main', {
state() {
// state表示这个store里的状态,也就是存放数据的地方
return {
name: '张三',
age:'26'
}
},
actions: {
// actions 相当于组件中的 methods
updateAge(data) {
this.age = data // 可以使用this访问和修改state中的数据
},
},
})
二、引用store.js
import {
useStore} from "../store/store" // 引入store
const store = useStore()
三、点击修改state里的值
1. 单个修改
const edit = ()=>{
store.updateAge("32")
}
2.批量修改
const edit = ()=>{
store.$patch({
name:"李四",
age:"32"
})
}