问题描述:pinia使用setup函数创建store $reset()失效
解决方式
- 改用options
export const useUserStore = defineStore('userStore', {
state: () => ({
count: 0,
name: 'Jenny',
isAdmin: true,
items: new Array(),
HashChanged: true
})
})
- 重写一个$reset方法
import { createPinia } from 'pinia'
const pinia = createPinia()
pinia.use(({ store }) => {
const initialState = JSON.parse(JSON.stringify(store.$state));
store.$reset = () => {
store.$patch(initialState);
}
})
export default pinia