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

輸入評論人、評論內容,點擊發(fā)表評論,評論數(shù)據(jù)將保存到localStorage中,并刷新評論列表。
1.先組織出一個最新評論數(shù)據(jù)對象
var comment = {id:Date.now(), user:this.user, content:this.content}
2. 把得到的評論對象,保存到localStorage中
1.localStorage只支持存字符串數(shù)據(jù),保存先調用JSON.stringify轉為字符串
2.在保存最新的評論數(shù)據(jù)之前,要先從localStorage獲取到之前的評論數(shù)據(jù)(string)轉換為一個數(shù)組對象,然后把最新的評論,push到這個數(shù)組
3.如果獲取到的localStorage中的評論字符串為空,不存在,則可以返回一個'[]'讓JSON.parse去轉換
4.把最新的評論列表數(shù)組,再次調用JOSN.stringify轉為數(shù)組字符串,然后調用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>評論內容:</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ù),先調用JSON.stringify
// 3.2 在保存最新的評論數(shù)據(jù)之前,要先從localStorage獲取到之前的評論數(shù)據(jù)(string)轉換為一個數(shù)組對象,然后把最新的評論,push到這個數(shù)組
// 3.3 如果獲取到的localStorage中的評論字符串為空,不存在,則可以返回一個'[]'讓JSON.parse去轉換
// 3.4 把最新的評論列表數(shù)組,再次調用JOSN.stringify轉為數(shù)組字符串,然后調用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ù)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 在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使用方法詳解
相關文章
使用vuex較為優(yōu)雅的實現(xiàn)一個購物車功能的示例代碼
這篇文章主要介紹了使用vuex較為優(yōu)雅的實現(xiàn)一個購物車功能的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12
vue keep-alive實現(xiàn)多組件嵌套中個別組件存活不銷毀的操作
這篇文章主要介紹了vue keep-alive實現(xiàn)多組件嵌套中個別組件存活不銷毀的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Vue在chrome44偶現(xiàn)點擊子元素事件無法冒泡的解決方法
這篇文章主要給大家介紹了關于Vue在chrome44偶現(xiàn)點擊子元素事件無法冒泡的解決方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12

