Vue使用localStorage存儲(chǔ)數(shù)據(jù)的方法
本文實(shí)例為大家分享了Vue使用localStorage存儲(chǔ)數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
通過下面這個(gè)案例來了解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ù)對象
var comment = {id:Date.now(), user:this.user, content:this.content}
2. 把得到的評(píng)論對象,保存到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ù)組對象,然后把最新的評(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ù)對象 //3.想辦法,把第二步得到的評(píng)論對象,保持到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ù)組對象,然后把最新的評(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>
可以打開開發(fā)者模式查看localStorage保存的數(shù)據(jù)
以上就是本文的全部內(nèi)容,希望對大家的學(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保存用戶登錄信息
- 詳解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)聽元素寬度變化方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12使用Element進(jìn)行前端開發(fā)的詳細(xì)圖文教程
眾所周知Element是一套Vue.js后臺(tái)組件庫,它能夠幫助你更輕松更快速地開發(fā)后臺(tái)項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于使用Element進(jìn)行前端開發(fā)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11使用elementUI table展開行內(nèi)嵌套table問題
這篇文章主要介紹了使用elementUI table展開行內(nèi)嵌套table問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04element-plus 如何設(shè)置 el-date-picker 彈出框位置
el-date-picker 組件會(huì)自動(dòng)根據(jù)空間范圍進(jìn)行選擇比較好的彈出位置,但特定情況下,它自動(dòng)計(jì)算出的彈出位置并不符合我們的實(shí)際需求,故需要我們手動(dòng)設(shè)置,這篇文章主要介紹了element-plus 如何設(shè)置 el-date-picker 彈出框位置,需要的朋友可以參考下2024-07-07在vue中實(shí)現(xiàn)禁止回退上一步,路由不存歷史記錄
這篇文章主要介紹了在vue中實(shí)現(xiàn)禁止回退上一步,路由不存歷史記錄,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07el-input寬度跟隨輸入內(nèi)容自適應(yīng)的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于el-input寬度跟隨輸入內(nèi)容自適應(yīng)的實(shí)現(xiàn)方法,我們再實(shí)際應(yīng)用中可能需要input文本框能夠根據(jù)輸入字符的所占據(jù)的寬度自動(dòng)調(diào)節(jié)尺寸,需要的朋友可以參考下2023-08-08vue+element+springboot實(shí)現(xiàn)文件下載進(jìn)度條展現(xiàn)功能示例
本文主要介紹了vue + element-ui + springboot 實(shí)現(xiàn)文件下載進(jìn)度條展現(xiàn)功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11