当以setup方式编写pinia 的store代码时,会提示"Error: Store *** is built using the setup syntax and does not implement $reset()";
此时需要使用pinia的插件,将reset方法添加到所有的store里面。
storeReset.ts
import cloneDeep from 'lodash.clonedeep';
export default function storeReset({ store }) {
const initialState = cloneDeep(store.$state);
store.$reset = () => store.$patch(cloneDeep(initialState));
}
main.ts
*******
import { createPinia } from 'pinia';
import StoreReset from '@/plugins/storeReset';
*******
*******
*******
*******
*******
const pinia = createPinia();
pinia.use(StoreReset);
*******
*******
*******
这样,就能放心的调用store.$reset()了。