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>

 

 

posted @ 2023-10-12 16:48  CodeProducter  阅读(38)  评论(0编辑  收藏  举报