dishait / pinia-plugin-unistorage

uniapp 下 pinia 的本地数据缓存插件
MIT License
17 stars 2 forks source link

无法在APP端运行 #1

Closed BTMuli closed 1 year ago

BTMuli commented 1 year ago

环境:

相关代码:

// store/index.js
import { createPinia } from 'pinia';
import { createUnistorage } from 'pinia-plugin-unistorage';

const store = new createPinia();

store.use(
    createUnistorage({
        serializer: {
            serialize: JSON.stringify,
            deserialize: JSON.parse,
        },
    })
);

export default store;
// main.js
import { createSSRApp } from 'vue';
import App from '@/App.vue';
import store from '@/store';

export function createApp() {
    const app = createSSRApp(App);

    app.use(store);

    return {
        app,
        store,
    };
}
// store/modules/app.js
import { defineStore } from 'pinia';

const useAppStore = defineStore({
    id: 'app-store',
    state: () => ({
        token: '',
    }),
    getters: {},
    actions: {
        setToken(data) {
            this.token = data;
        }
    unistorage: true
});
export default useAppStore;

执行安卓实机调试时终端报错

reportJSException >>>> exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->
 at useStore (app-service.js:5068:15)
 at  (app-service.js:5192:22)
 at  (app-service.js:10250:3)

进行 debug 后 console 显示信息: image

尝试将 https://www.npmjs.com/package/pinia-plugin-unistorage 的 main.js 覆盖,仍旧报错

markthree commented 1 year ago

@BTMuli 你好,请使用 uniapp 独有的使用方式,具体可见 👉 使用

BTMuli commented 1 year ago

经过测试发现采用 README 中的引用方式无法解决,该报错是因为在某页面使用 useStore() 位于 data() 之前导致的,将 useStore 放在 methods 内即可解决, 2Y}5WSF AD0HA$LVM6K8IVL 但其他小程序端跟H5端采用改写法并未出现问题。

markthree commented 1 year ago

@BTMuli 文档有对应描述 👉 outside-component-usage

BTMuli commented 1 year ago

我懂了,这是因为安卓应用没有 SSR 渲染吗?