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

輸入評(píng)論人、評(píng)論內(nèi)容,點(diǎn)擊發(fā)表評(píng)論,評(píng)論數(shù)據(jù)將保存到localStorage中,并刷新評(píng)論列表。
1.先組織出一個(gè)最新評(píng)論數(shù)據(jù)對(duì)象
var comment = {id:Date.now(), user:this.user, content:this.content}
2. 把得到的評(píng)論對(duì)象,保存到localStorage中
1.localStorage只支持存字符串?dāng)?shù)據(jù),保存先調(diào)用JSON.stringify轉(zhuǎn)為字符串
2.在保存最新的評(píng)論數(shù)據(jù)之前,要先從localStorage獲取到之前的評(píng)論數(shù)據(jù)(string)轉(zhuǎn)換為一個(gè)數(shù)組對(duì)象,然后把最新的評(píng)論,push到這個(gè)數(shù)組
3.如果獲取到的localStorage中的評(píng)論字符串為空,不存在,則可以返回一個(gè)'[]'讓JSON.parse去轉(zhuǎn)換
4.把最新的評(píng)論列表數(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">評(píng)論人:{{item.user}}</span>
{{item.content}}
</li>
</ul>
</div>
<template id="tmp1">
<div>
<div class="form-group">
<label>評(píng)論人:</label>
<input type="text" v-model="user" class="form-control">
</div>
<div class="form-group">
<label>評(píng)論內(nèi)容:</label>
<textarea class="form-control" v-model="content"></textarea>
</div>
<div class="form-group">
<input type="button" value="發(fā)表評(píng)論" 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.評(píng)論數(shù)據(jù)存到哪里去,存放到了localStorage中
//2.先組織出一個(gè)最新評(píng)論數(shù)據(jù)對(duì)象
//3.想辦法,把第二步得到的評(píng)論對(duì)象,保持到localStorage中】
// 3.1 localStorage只支持存字符串?dāng)?shù)據(jù),先調(diào)用JSON.stringify
// 3.2 在保存最新的評(píng)論數(shù)據(jù)之前,要先從localStorage獲取到之前的評(píng)論數(shù)據(jù)(string)轉(zhuǎn)換為一個(gè)數(shù)組對(duì)象,然后把最新的評(píng)論,push到這個(gè)數(shù)組
// 3.3 如果獲取到的localStorage中的評(píng)論字符串為空,不存在,則可以返回一個(gè)'[]'讓JSON.parse去轉(zhuǎn)換
// 3.4 把最新的評(píng)論列表數(shù)組,再次調(diào)用JOSN.stringify轉(zhuǎn)為數(shù)組字符串,然后調(diào)用localStorage.setItem()
var comment = {id:Date.now(), user:this.user, content:this.content}
//從localStorage中獲取所用的評(píng)論
var list = JSON.parse(localStorage.getItem("cmts") || '[]')
list.unshift(comment)
//重新保存最新的評(píng)論數(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中,加載評(píng)論列表
loadComments(){
var list = JSON.parse(localStorage.getItem("cmts") || '[]')
this.list = list
}
},
created(){
this.loadComments()
},
components:{
'cmt-box':conmmentBox
}
})
</script>
</html>
可以打開(kāi)開(kāi)發(fā)者模式查看localStorage保存的數(shù)據(jù)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 在Vue3中使用localStorage保存數(shù)據(jù)的流程步驟
- 教你在Vue3中使用useStorage輕松實(shí)現(xiàn)localStorage功能
- VUE使用localstorage和sessionstorage實(shí)現(xiàn)登錄示例詳解
- vue如何使用cookie、localStorage和sessionStorage進(jìn)行儲(chǔ)存數(shù)據(jù)
- vue使用localStorage保存登錄信息 適用于移動(dòng)端、PC端
- Vue項(xiàng)目使用localStorage+Vuex保存用戶(hù)登錄信息
- 詳解vue中l(wèi)ocalStorage的使用方法
- 詳解Vue中l(wèi)ocalstorage和sessionstorage的使用
- vue中的localStorage使用方法詳解
相關(guān)文章
在瀏覽器console中如何調(diào)用vue內(nèi)部方法
這篇文章主要介紹了在瀏覽器console中如何調(diào)用vue內(nèi)部方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue中用qrcode庫(kù)將超鏈接生成二維碼圖片的示例代碼
生成二維碼是一種常見(jiàn)的需求,無(wú)論是用于商業(yè)宣傳還是個(gè)人分享,二維碼都可以提供快速方便的方式來(lái)傳遞信息,在Vue框架中,我們可以使用qrcode庫(kù)來(lái)輕松地生成二維碼,本篇博文將介紹如何安裝qrcode庫(kù),并通過(guò)一個(gè)實(shí)際例子來(lái)展示如何生成二維碼,需要的朋友可以參考下2023-12-12
使用vuex較為優(yōu)雅的實(shí)現(xiàn)一個(gè)購(gòu)物車(chē)功能的示例代碼
這篇文章主要介紹了使用vuex較為優(yōu)雅的實(shí)現(xiàn)一個(gè)購(gòu)物車(chē)功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Vue數(shù)據(jù)驅(qū)動(dòng)模擬實(shí)現(xiàn)2
這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動(dòng)模擬實(shí)現(xiàn)的相關(guān)資料,實(shí)現(xiàn)Observer構(gòu)造函數(shù),監(jiān)聽(tīng)已有數(shù)據(jù)data中的所有屬性,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
vue keep-alive實(shí)現(xiàn)多組件嵌套中個(gè)別組件存活不銷(xiāo)毀的操作
這篇文章主要介紹了vue keep-alive實(shí)現(xiàn)多組件嵌套中個(gè)別組件存活不銷(xiāo)毀的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
vue的diff算法知識(shí)點(diǎn)總結(jié)
本篇文章給大家分享了關(guān)于vue的diff算法的相關(guān)知識(shí)點(diǎn)總結(jié),有興趣的朋友參考學(xué)習(xí)下。2018-03-03
Vue在chrome44偶現(xiàn)點(diǎn)擊子元素事件無(wú)法冒泡的解決方法
這篇文章主要給大家介紹了關(guān)于Vue在chrome44偶現(xiàn)點(diǎn)擊子元素事件無(wú)法冒泡的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
解決vue中使用swiper 插件出錯(cuò)的問(wèn)題
這篇文章主要介紹了vue中使用swiper 插件出錯(cuò)問(wèn)題及解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08

