pinia数据持久化,定义persist时,报红线,打包也报错?

pinia代码部分

image.png

家人们,有没有知道该怎么处理的啊,我试着把这段函数注释了,就能打包了,但是就是报错显示,

下面是报错的提示信息,

没有与此调用匹配的重载。
  第 1 个重载(共 3 个),“(id: "dataStore", options: Omit<DefineStoreOptions<"dataStore", { xiaohu: string; userinfo: {}; asideWidth: string; tabList: { path: string; title: string; }[]; activeTab: string; }, {}, { handleAsideWidth(): void; getDefault(route: string): void; addTabs(tab: any): void; }>, "id">): StoreDefinition<...>”,出现以下错误。
    类型“{ persist: { enabled: boolean; strategies: { storage: Storage; paths: string[]; }[]; }; state: () => { xiaohu: string; userinfo: {}; asideWidth: string; tabList: { path: string; title: string; }[]; activeTab: string; }; actions: { ...; }; getters: {}; }”的参数不能赋给类型“Omit<DefineStoreOptions<"dataStore", { xiaohu: string; userinfo: {}; asideWidth: string; tabList: { path: string; title: string; }[]; activeTab: string; }, {}, { handleAsideWidth(): void; getDefault(route: string): void; addTabs(tab: any): void; }>, "id">”的参数。
      对象字面量只能指定已知属性,并且“persist”不在类型“Omit<DefineStoreOptions<"dataStore", { xiaohu: string; userinfo: {}; asideWidth: string; tabList: { path: string; title: string; }[]; activeTab: string; }, {}, { handleAsideWidth(): void; getDefault(route: string): void; addTabs(tab: any): void; }>, "id">”中。
  第 2 个重载(共 3 个),“(id: "dataStore", storeSetup: () => unknown, options?: DefineSetupStoreOptions<"dataStore", _UnwrapAll<Pick<unknown, never>>, Pick<unknown, never>, Pick<unknown, never>> | undefined): StoreDefinition<...>”,出现以下错误。
    类型“{ persist: { enabled: boolean; strategies: { storage: Storage; paths: string[]; }[]; }; state: () => { xiaohu: string; userinfo: {}; asideWidth: string; tabList: { path: string; title: string; }[]; activeTab: string; }; actions: { ...; }; getters: {}; }”的参数不能赋给类型“() => unknown”的参数。
      对象字面量只能指定已知属性,并且“persist”不在类型“() => unknown”中。ts(2769)
(property) persist: {
    enabled: boolean;
    strategies: {
        storage: Storage;
        paths: string[];
    }[];
}

下面是将里面的代码部分注释掉了,还是会报这个错误的情况**

image.png

没有与此调用匹配的重载。
  第 1 个重载(共 3 个),“(id: "dataStore", options: Omit<DefineStoreOptions<"dataStore", { xiaohu: string; userinfo: {}; asideWidth: string; tabList: { path: string; title: string; }[]; activeTab: string; }, {}, { handleAsideWidth(): void; getDefault(route: string): void; addTabs(tab: any): void; }>, "id">): StoreDefinition<...>”,出现以下错误。
    类型“{ persist: {}; state: () => { xiaohu: string; userinfo: {}; asideWidth: string; tabList: { path: string; title: string; }[]; activeTab: string; }; actions: { handleAsideWidth(): void; getDefault(route: string): void; addTabs(tab: any): void; }; getters: {}; }”的参数不能赋给类型“Omit<DefineStoreOptions<"dataStore", { xiaohu: string; userinfo: {}; asideWidth: string; tabList: { path: string; title: string; }[]; activeTab: string; }, {}, { handleAsideWidth(): void; getDefault(route: string): void; addTabs(tab: any): void; }>, "id">”的参数。
      对象字面量只能指定已知属性,并且“persist”不在类型“Omit<DefineStoreOptions<"dataStore", { xiaohu: string; userinfo: {}; asideWidth: string; tabList: { path: string; title: string; }[]; activeTab: string; }, {}, { handleAsideWidth(): void; getDefault(route: string): void; addTabs(tab: any): void; }>, "id">”中。
  第 2 个重载(共 3 个),“(id: "dataStore", storeSetup: () => unknown, options?: DefineSetupStoreOptions<"dataStore", _UnwrapAll<Pick<unknown, never>>, Pick<unknown, never>, Pick<unknown, never>> | undefined): StoreDefinition<...>”,出现以下错误。
    类型“{ persist: {}; state: () => { xiaohu: string; userinfo: {}; asideWidth: string; tabList: { path: string; title: string; }[]; activeTab: string; }; actions: { handleAsideWidth(): void; getDefault(route: string): void; addTabs(tab: any): void; }; getters: {}; }”的参数不能赋给类型“() => unknown”的参数。
      对象字面量只能指定已知属性,并且“persist”不在类型“() => unknown”中。ts(2769)

成功了!

时隔多日,突然又看到了有人回复我这个问题,然后把持久化插件(pinia-plugin-persist)卸载了,配置也删了,安装了pinia-plugin-persistedstate,这个插件配置好,就可以了。下面是没有报错,并且打包成功的截图!!!

成功截图

非常感谢各位的帮助!!!

阅读 8.4k
3 个回答

image.png

image.png

查看 Pinia 配置项 以及 PersistedStateOptions 类型声明可知,不存在 enabled 以及 strateies 属性,正确写法如下:

export const useStore = defineStore("main", {
  state: () => ({
    someState: "你好 pinia"
  }),
  // 注意这里
  persist: {
    storage: localStorage,
    paths: []
  }
});

PersistedStateOptions 类型声明如下:

interface PersistedStateOptions {
    /**
     * Storage key to use.
     * @default $store.id
     */
    key?: string | ((id: string) => string);
    /**
     * Where to store persisted state.
     * @default localStorage
     */
    storage?: StorageLike;
    /**
     * Dot-notation paths to partially save state. Saves everything if undefined.
     * @default undefined
     */
    paths?: Array<string>;
    /**
     * Customer serializer to serialize/deserialize state.
     */
    serializer?: Serializer;
    /**
     * Hook called before state is hydrated from storage.
     * @default null
     */
    beforeRestore?: (context: PiniaPluginContext) => void;
    /**
     * Hook called after state is hydrated from storage.
     * @default undefined
     */
    afterRestore?: (context: PiniaPluginContext) => void;
    /**
     * Logs errors in console when enabled.
     * @default false
     */
    debug?: boolean;
}
新手上路,请多包涵

这样就可以了 !
"pinia-plugin-persistedstate": "^3.2.1",

export const useCtiEvent = defineStore("ctiEvent", () => {
    let status:Ref<string | null> = ref(null)
    return {
        status
    }
},{
  persist:{
    key:"my_ctiEvents",
    storage:sessionStorage,
    paths:['status']
  }
}
}

image.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏