vuex-persistedstate 在UniApp报错的问题(‘setItem‘ of undefined )

遇到问题不害怕,无需着急;冷静下来慢慢分析、定位问题。

报错

起因:真机调试运行时报错:

reportJSException >>>> exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->Uncaught TypeError: Cannot read property 'setItem' of undefined  

这个问题并不会在网页调试、小程序调试中出现,只会在真机调试中出现。单看报错信息也是非常的懵:拿到两个关键词setItemundefined。太模糊了,根本没办法定位。

具体排查过程不详细说了(花费了不少时间最后才定位到时vuex-persistedstate的问题)。此文主要是总结记录、分享。

看一下报错的写法:
报错的写法
一般开发 网页项目 这样写是不会有问题的。但是 !!!
到了App环境就会出现上面莫名的问题。

问题根源

原因在于 vuex-persistedstate默认配置是使用 window.localStorage
为了求证特意去看了源码,仓库地址 vuex-persistedstate
源码也非常少,只有一个index.ts文件。

// 源码34行:
const storage = options.storage || (window && window.localStorage);

// 源码53行
function setState(key, state, storage) {
    return storage.setItem(key, JSON.stringify(state));
}

在栏看看真机运行时候window是个啥,打印输出:
打印window

真机运行时候window对象是undefined

问题根源找到了~~

解决方法

persistedState是接收一个 Options 参数的。把 Storage 的参数写一下,用上 uniapp特有的api

    import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
//引入vuex-persistedstate
import persistedState from "vuex-persistedstate";
import pricedetail from './pricedetail.js'

console.log(' =====> window', window);
const store = new Vuex.Store({
	state: pricedetail.state,
	getters: pricedetail.getters,
	mutations: pricedetail.mutations,
	actions: pricedetail.actions,

	plugins: [persistedState({
        // 传入参数
		storage: {
			getItem: (key) => uni.getStorageSync(key),
			setItem: (key, val) => uni.setStorageSync(key, value),
			removeItem: (key) => uni.removeStorageSync(key)
		}
	})]

})

export default store


end

  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
可以在uniapp中使用vuex-persistedstate来实现持久化存储,具体操作步骤如下: 1. 安装vuex-persistedstate:在uniapp项目中进入终端,执行npm install vuex-persistedstate --save命令进行安装。 2. 在store目录下创建一个plugins.js文件,将以下代码复制到文件中: ``` import createPersistedState from 'vuex-persistedstate' export default ({store}) => { createPersistedState({ key: 'uniapp-vuex', storage: { getItem: key => uni.getStorageSync(key), setItem: (key, value) => uni.setStorageSync(key, value), removeItem: key => uni.removeStorageSync(key) } })(store) } ``` 3. 在store目录下的index.js文件中引入plugins.js,并将其作为store的插件: ``` import Vue from 'vue' import Vuex from 'vuex' import plugins from './plugins' Vue.use(Vuex) const store = new Vuex.Store({ plugins, ... }) export default store ``` 4. 至此,使用vuex-persistedstate实现持久化存储的配置已完成。在需要存储的state属性中添加@Persist注解即可。 例如,在store目录下的index.js文件中: ``` import {Persist} from 'vuex-persistedstate' const state = { count: 0 } const mutations = { increment (state) { state.count++ } } const actions = { increment ({commit}) { commit('increment') } } export default { state, mutations, actions, plugins: [Persist('count')] } ``` 在以上代码中,通过@Persist('count')表示count属性需要进行持久化存储。 5. 至此,在uniapp中使用vuex-persistedstate实现持久化存储的配置已完成,可以在需要的地方进行使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值