【Vue/js】通过localStorage浏览器实现变量和对象的本地缓存(图文+完整源代码)
2023-09-11 14:15:10 时间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>demo</title>
</head>
<body>
<div id="box">
<h2>通过localStorage把变量或对象存入浏览器缓存</h2>
<button onclick="save()"> 存入浏览器</button>
<button onclick="get()"> 调取浏览器参数</button>
</div>
</body>
<script type="text/javascript">
function save() {
let p = { name: '张三丰', sex: '男' }
localStorage.setItem('username', 'csj')
localStorage.setItem('userId', '10010')
localStorage.setItem('info', JSON.stringify(p))
}
function get() {
console.log('=============')
console.log('username:'+localStorage.getItem('username'))
console.log('userId:'+localStorage.getItem('userId'))
console.log('info:'+JSON.parse(localStorage.getItem('info')).name)
let p = { name: '张三丰', sex: '男' }
localStorage.setItem('username', 'csj')
localStorage.setItem('userId', '10010')
localStorage.setItem('info', JSON.stringify(p))
}
</script>
</html>
相关文章
- 结合Vue.js的前端压缩图片方案
- [Nuxt] Add Arrays of Data to the Vuex Store and Display Them in Vue.js Templates
- [Vue-rx] Share RxJS Streams to Avoid Multiple Requests in Vue.js
- [Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js
- [Vue] Dynamic Vue.js Components with the component element
- vue.js3:分享到微信好友/朋友圈(卡片形式/图片文字形式)(vue@3.2.33 / weixin-js-sdk@1.6.0)
- vue.js 3.2.22:菜单每行依次滑动出现的动画
- JS:crypto-js模块实现数据加密解密
- 计算机毕设 SSM Vue的网上购物商城系统(含源码+论文)
- 创建Vue工程常用的命令
- vue-tour快速入门:VUE项目中如何使用vue-tour新手引导指引插件?怎样使用?包含具体实例
- vue-ant design示例大全——icon与分隔符本地css/js资源
- vue-ant design示例大全——按钮本地css/js资源