- 创建文件src/store/example.js
import { defineStore } from 'pinia';
// 创建 store
export const useMyStore = defineStore('myStore', {
state: () => ({
data: '',
}),
getters: {
getData: state => state.data,
},
actions: {
updateData(newData) {
this.data = newData;
},
},
});
2.在需要存储数据的文件中
import { useMyStore } from '@/store/example.js';
const store = useMyStore();
//在需要存储数据的地方
store.updateData(123);
3.在需要获取所存储的数据的文件中
import { useMyStore } from '@/store/example.js';
const store = useMyStore();
const data = store.getData;
console.log(data) // 123

本文介绍了如何在Vue应用中使用Pinia库创建一个名为myStore的存储模块,包括定义状态、getter和action。展示了如何在需要时更新和获取存储的数据。
1695

被折叠的 条评论
为什么被折叠?



