Vue使用localStorage存儲數(shù)據(jù)的方法
本文實例為大家分享了Vue使用localStorage存儲數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
通過下面這個案例來了解localStorage的基本使用方法。

輸入評論人、評論內(nèi)容,點擊發(fā)表評論,評論數(shù)據(jù)將保存到localStorage中,并刷新評論列表。
1.先組織出一個最新評論數(shù)據(jù)對象
var comment = {id:Date.now(), user:this.user, content:this.content}
2. 把得到的評論對象,保存到localStorage中
1.localStorage只支持存字符串數(shù)據(jù),保存先調(diào)用JSON.stringify轉(zhuǎn)為字符串
2.在保存最新的評論數(shù)據(jù)之前,要先從localStorage獲取到之前的評論數(shù)據(jù)(string)轉(zhuǎn)換為一個數(shù)組對象,然后把最新的評論,push到這個數(shù)組
3.如果獲取到的localStorage中的評論字符串為空,不存在,則可以返回一個'[]'讓JSON.parse去轉(zhuǎn)換
4.把最新的評論列表數(shù)組,再次調(diào)用JOSN.stringify轉(zhuǎn)為數(shù)組字符串,然后調(diào)用localStorage.setItem()保存
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../css/bootstrap.css" rel="external nofollow" >
</head>
<body>
<div id='app'>
<cmt-box @func="loadComments"></cmt-box>
<ul class="list-group">
<li class="list-group-item" v-for="item in list" :key="item.id">
<span class="badge">評論人:{{item.user}}</span>
{{item.content}}
</li>
</ul>
</div>
<template id="tmp1">
<div>
<div class="form-group">
<label>評論人:</label>
<input type="text" v-model="user" class="form-control">
</div>
<div class="form-group">
<label>評論內(nèi)容:</label>
<textarea class="form-control" v-model="content"></textarea>
</div>
<div class="form-group">
<input type="button" value="發(fā)表評論" class="btn btn-primary" @click="postComment">
</div>
</div>
</template>
</body>
<script src="../lib/vue.js"></script>
<script>
var conmmentBox={
template:'#tmp1',
data(){
return{
user:'',
content:''
}
},
methods:{
postComment(){
//1.評論數(shù)據(jù)存到哪里去,存放到了localStorage中
//2.先組織出一個最新評論數(shù)據(jù)對象
//3.想辦法,把第二步得到的評論對象,保持到localStorage中】
// 3.1 localStorage只支持存字符串數(shù)據(jù),先調(diào)用JSON.stringify
// 3.2 在保存最新的評論數(shù)據(jù)之前,要先從localStorage獲取到之前的評論數(shù)據(jù)(string)轉(zhuǎn)換為一個數(shù)組對象,然后把最新的評論,push到這個數(shù)組
// 3.3 如果獲取到的localStorage中的評論字符串為空,不存在,則可以返回一個'[]'讓JSON.parse去轉(zhuǎn)換
// 3.4 把最新的評論列表數(shù)組,再次調(diào)用JOSN.stringify轉(zhuǎn)為數(shù)組字符串,然后調(diào)用localStorage.setItem()
var comment = {id:Date.now(), user:this.user, content:this.content}
//從localStorage中獲取所用的評論
var list = JSON.parse(localStorage.getItem("cmts") || '[]')
list.unshift(comment)
//重新保存最新的評論數(shù)據(jù)
localStorage.setItem('cmts',JSON.stringify(list))
this.user = this.content = ''
this.$emit('func')
}
}
}
var vm = new Vue({
el:'#app',
data:{
list:[]
},
methods:{
//從本地的localStorage中,加載評論列表
loadComments(){
var list = JSON.parse(localStorage.getItem("cmts") || '[]')
this.list = list
}
},
created(){
this.loadComments()
},
components:{
'cmt-box':conmmentBox
}
})
</script>
</html>
可以打開開發(fā)者模式查看localStorage保存的數(shù)據(jù)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
- 在Vue3中使用localStorage保存數(shù)據(jù)的流程步驟
- 教你在Vue3中使用useStorage輕松實現(xiàn)localStorage功能
- VUE使用localstorage和sessionstorage實現(xiàn)登錄示例詳解
- vue如何使用cookie、localStorage和sessionStorage進行儲存數(shù)據(jù)
- vue使用localStorage保存登錄信息 適用于移動端、PC端
- Vue項目使用localStorage+Vuex保存用戶登錄信息
- 詳解vue中l(wèi)ocalStorage的使用方法
- 詳解Vue中l(wèi)ocalstorage和sessionstorage的使用
- vue中的localStorage使用方法詳解
相關(guān)文章
vue使用element-resize-detector監(jiān)聽元素寬度變化方式
這篇文章主要介紹了vue使用element-resize-detector監(jiān)聽元素寬度變化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
使用elementUI table展開行內(nèi)嵌套table問題
這篇文章主要介紹了使用elementUI table展開行內(nèi)嵌套table問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
element-plus 如何設(shè)置 el-date-picker 彈出框位置
el-date-picker 組件會自動根據(jù)空間范圍進行選擇比較好的彈出位置,但特定情況下,它自動計算出的彈出位置并不符合我們的實際需求,故需要我們手動設(shè)置,這篇文章主要介紹了element-plus 如何設(shè)置 el-date-picker 彈出框位置,需要的朋友可以參考下2024-07-07
el-input寬度跟隨輸入內(nèi)容自適應(yīng)的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于el-input寬度跟隨輸入內(nèi)容自適應(yīng)的實現(xiàn)方法,我們再實際應(yīng)用中可能需要input文本框能夠根據(jù)輸入字符的所占據(jù)的寬度自動調(diào)節(jié)尺寸,需要的朋友可以參考下2023-08-08
vue+element+springboot實現(xiàn)文件下載進度條展現(xiàn)功能示例
本文主要介紹了vue + element-ui + springboot 實現(xiàn)文件下載進度條展現(xiàn)功能,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11

