pinia的使用
1. 安装pinia
npm i pinia -S
2. pinia定义仓库的声明及调用
1 // employee仓库的声明
2 import { defineStore } from "pinia";
3
4 export const useEmployeeStore = defineStore("employee", {
5 state: () => ({
6 employeeList: []
7 }),
8 getters: {
9 getEmployeeList(state) {
10 return state.employeeList;
11 }
12 },
13 actions: {
14 setEmployeeList(list) {
15 this.employeeList = list;
16 }
17 }
18 });
19
20
21 // 调用
22 <script setup>
23 import { useEmployeeStore } from "@/store/modules/employee.js";
24
25 // 获取声明的仓库
26 const store = useEmployeeStore();
27
28 // 获取仓库的属性
29 console.log(store.setEmployeeList([]), store.employeeList);
30 </script>