当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue 界面刷新数据被清除 localStorage的使用详解

vue 界面刷新数据被清除 localStorage的使用详解

2019年06月02日  | 移动技术网IT编程  | 我要评论

localstorage是html5新增的一个本地存储api,它有5m的大小空间,通过(key,value)的方式存储在浏览器中

window.localstorage.setitem('key', value); //储存文件

window.localstorage.getitem('key'); //读取文件

window.localstorage.removeitem('key'); //清除文件

vue中使用方法:

1、新建一个store.js文件

localstorage只能存储字符串,非字符串的数据在存储之前会被转换成字符串。在存储一些复杂数据类型时可能有些麻烦,下面方法是先使用json.stringfy()方法将其转换为字符串后存储,读取时使用json.parse()方法进行还原。

const idlist_key = 'idlist'; //定义常量保存键值

export default {

 saveidlist(data){

 window.localstorage.setitem(idlist_key,json.stringify(data)); 

 },

 fetchidlist(){

 return json.parse(window.localstorage.getitem(idlist_key)|| '[]');

 }

}

2、引入js文件:

3、使用:

store.saveidlist(selectidlist); //保存

selectidlist = store.fetchidlist(); //读取

以上这篇vue 界面刷新数据被清除 localstorage的使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网