项目场景:
- Vue3
- TS 语言
- Pinia
- 插件:pinia-plugin-persist
问题描述
代码如下:
import { defineStore } from 'pinia'
export const useInfoStore = defineStore('info', {
state: () => {
return {
activeIndex: 0
}
},
actions: {
updateIndex(active: number) {
this.activeIndex = active
}
},
// 注意这里报错 // 没有与此调用匹配的重载
persist: {
storage: localStorage
}
})
解决方案:
- 卸载聪(S) 明(B) 的 pinia-plugin-persist 插件
pnpm uninstall pinia-plugin-persist
- 安装替换
pnpm install pinia-plugin-persistedstate
在 main.ts
中配置 ,大概代码如下
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
const app = createApp(App)
const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)