Vue?瀏覽器本地存儲的問題小結(jié)
localstorage
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>瀏覽器本地存儲</title>
</head>
<body>
<div id="root">
<button onclick="saveData()">點我保存一個數(shù)據(jù)</button>
<button onclick="readData()">點我讀取一個數(shù)據(jù)</button>
<button onclick="deleteData()">點我刪除一個數(shù)據(jù)</button>
<button onclick="deleteAllData()">點我清空數(shù)據(jù)</button>
</div>
<script type="text/javascript">
let person = {name:"張三",age:"18"}
function saveData() {
localStorage.setItem("msg","hello")
localStorage.setItem("msg2",666)
localStorage.setItem("msg3",JSON.stringify(person))
}
function readData(){
console.log(localStorage.getItem("msg"))
console.log(localStorage.getItem("msg2"))
const result = localStorage.getItem("msg3")
console.log(result)
console.log(JSON.parse(result))
}
function deleteData(){
localStorage.removeItem("msg")
}
function deleteAllData(){
localStorage.clear()
}
</script>
</body>
</html>

SessionStorage
和 LocalStorage 用法相同,把上邊代碼中的 localStorage改為sessionStorage

總結(jié)
LocalStorage 和 SessionStorage 統(tǒng)稱為 WebStorage
1.存儲內(nèi)容大小一般支持5MB左右(不同瀏覽器可能還不一樣)
⒉瀏覽器端通過 Window.sessionStorage 和Window.localStorage屬性來實現(xiàn)本地存儲機(jī)制
3.相關(guān)API:
1.xxxxxStorage.setItem( " key’ , “value”);
該方法接受一個鍵和值作為參數(shù),會把鍵值對添加到存儲中,如果鍵名存在,則更新其對應(yīng)的值
2.xxxxxStorage.getItem( “person”);
該方法接受一個鍵名作為參數(shù),返回健名對應(yīng)的值
3.xxxxxStorage.removeItem( “key”);
該方法接受一個鍵名作為參數(shù),并把該鍵名從存儲中刪除
4.xxxxxStorage.clear()
該方法會清空存儲中的所有數(shù)據(jù)
4.備注:
1.SessionStorage 存儲的內(nèi)容會隨著瀏覽器窗口關(guān)閉而消失2.LocalStorage 存儲的內(nèi)容,需要手動清除才會消失(調(diào)用api 或 清空緩存)
3. xxxxStorage.getItem(xxx),如果 xxx 對應(yīng)的 value 獲取不到,那么 getltem 的返回值是null
4.JSON.parse(null) 的結(jié)果依然是 null
TodoList 改為本地存儲
我們之前寫的 TodoList 案例數(shù)據(jù)是寫死的,每次刷新都恢復(fù)到寫死的數(shù)據(jù),我們現(xiàn)在把它改為本地存儲。修改 App.vue,把 todos 改為深度監(jiān)視,每當(dāng) todos 發(fā)生變化就使用本地存儲存儲數(shù)據(jù)。同時初始化的時候,todos 賦值是從本地存儲讀取的
......
<script>
......
export default {
......
data() {
return {
//讀取本地存儲
todos: JSON.parse(localStorage.getItem("todos")) || []
}
},
methods: {
......
},
watch:{
//深度監(jiān)視
todos:{
deep:true,
handler(value){
localStorage.setItem("todos",JSON.stringify(value))
}
}
}
}
</script>
......
運行程序,輸入數(shù)據(jù),刷新瀏覽器,數(shù)據(jù)不會消失

到此這篇關(guān)于Vue 瀏覽器本地存儲的文章就介紹到這了,更多相關(guān)Vue 瀏覽器本地存儲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element?el-tree折疊收縮的實現(xiàn)示例
本文主要介紹了element?el-tree折疊收縮的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue鼠標(biāo)移入添加class樣式,鼠標(biāo)移出去除樣式(active)實現(xiàn)方法
今天小編就為大家分享一篇vue鼠標(biāo)移入添加class樣式,鼠標(biāo)移出去除樣式(active)實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue登錄頁面回車執(zhí)行事件@keyup.enter.native問題
這篇文章主要介紹了vue登錄頁面回車執(zhí)行事件@keyup.enter.native問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
vue兩組件間值傳遞 $router.push實現(xiàn)方法
兩組件間傳值,可能包含多種情況,這篇文章主要介紹了vue兩組件間值傳遞 $router.push實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
vue實現(xiàn)excel文件導(dǎo)入導(dǎo)出操作示例
這篇文章主要為大家介紹了vue實現(xiàn)excel文件的導(dǎo)入導(dǎo)出實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

