vue中的localStorage使用方法詳解
在 Vue 項(xiàng)目里能夠直接使用 localStorage,因?yàn)?localStorage 是瀏覽器提供的 Web Storage API 的一部分,它獨(dú)立于 JavaScript 框架,所以可以在 Vue 項(xiàng)目的任何地方使用,包括組件的模板、script 標(biāo)簽內(nèi)部,無論是 Vue 2 還是 Vue 3 都適用。下面分別介紹在 Vue 2 和 Vue 3 里使用 localStorage 的方法。
在 Vue 2 中使用 localStorage
保存數(shù)據(jù)到 localStorage
<template>
<div>
<button @click="saveData">保存數(shù)據(jù)到 localStorage</button>
</div>
</template>
<script>
export default {
methods: {
saveData() {
const data = { message: '這是要保存的數(shù)據(jù)' };
// 將對象轉(zhuǎn)換為 JSON 字符串
const jsonData = JSON.stringify(data);
// 保存到 localStorage
localStorage.setItem('myData', jsonData);
console.log('數(shù)據(jù)已保存到 localStorage');
}
}
};
</script>從 localStorage 讀取數(shù)據(jù)
<template>
<div>
<button @click="getData">從 localStorage 讀取數(shù)據(jù)</button>
<p v-if="data">讀取到的數(shù)據(jù): {{ data.message }}</p>
</div>
</template>
<script>
export default {
data() {
return {
data: null
};
},
methods: {
getData() {
// 從 localStorage 讀取數(shù)據(jù)
const jsonData = localStorage.getItem('myData');
if (jsonData) {
// 將 JSON 字符串轉(zhuǎn)換為對象
this.data = JSON.parse(jsonData);
console.log('從 localStorage 讀取到數(shù)據(jù):', this.data);
} else {
console.log('localStorage 中沒有找到對應(yīng)數(shù)據(jù)');
}
}
}
};
</script>刪除 localStorage 中的數(shù)據(jù)
<template>
<div>
<button @click="removeData">刪除 localStorage 中的數(shù)據(jù)</button>
</div>
</template>
<script>
export default {
methods: {
removeData() {
// 刪除 localStorage 中的指定數(shù)據(jù)
localStorage.removeItem('myData');
console.log('localStorage 中的數(shù)據(jù)已刪除');
}
}
};
</script>在 Vue 3 中使用 localStorage
保存數(shù)據(jù)到 localStorage
<template>
<div>
<button @click="saveData">保存數(shù)據(jù)到 localStorage</button>
</div>
</template>
<script setup>
import { ref } from 'vue';
const saveData = () => {
const data = { message: '這是要保存的數(shù)據(jù)' };
const jsonData = JSON.stringify(data);
localStorage.setItem('myData', jsonData);
console.log('數(shù)據(jù)已保存到 localStorage');
};
</script>刪除 localStorage 中的數(shù)據(jù)
<template>
<div>
<button @click="removeData">刪除 localStorage 中的數(shù)據(jù)</button>
</div>
</template>
<script setup>
const removeData = () => {
localStorage.removeItem('myData');
console.log('localStorage 中的數(shù)據(jù)已刪除');
};
</script>注意事項(xiàng)
localStorage 只能存儲字符串類型的數(shù)據(jù),所以在保存對象或數(shù)組時,需要先使用 JSON.stringify() 方法將其轉(zhuǎn)換為 JSON 字符串,讀取時再使用 JSON.parse() 方法將其轉(zhuǎn)換回對象或數(shù)組。
localStorage 存儲的數(shù)據(jù)會一直保留在瀏覽器中,除非手動刪除,并且存儲大小通常限制在 5MB 左右。
在使用 localStorage 時,要注意數(shù)據(jù)的安全性,避免存儲敏感信息
到此這篇關(guān)于vue里localStorage可以直接用嗎的文章就介紹到這了,更多相關(guān)vue localStorage內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在Vue3中使用localStorage保存數(shù)據(jù)的流程步驟
- 教你在Vue3中使用useStorage輕松實(shí)現(xiàn)localStorage功能
- VUE使用localstorage和sessionstorage實(shí)現(xiàn)登錄示例詳解
- vue如何使用cookie、localStorage和sessionStorage進(jìn)行儲存數(shù)據(jù)
- vue使用localStorage保存登錄信息 適用于移動端、PC端
- Vue使用localStorage存儲數(shù)據(jù)的方法
- Vue項(xiàng)目使用localStorage+Vuex保存用戶登錄信息
- 詳解vue中l(wèi)ocalStorage的使用方法
- 詳解Vue中l(wèi)ocalstorage和sessionstorage的使用
相關(guān)文章
淺談vue項(xiàng)目4rs vue-router上線后history模式遇到的坑
今天小編就為大家分享一篇淺談vue項(xiàng)目4rs vue-router上線后history模式遇到的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue3在css中使用v-bind綁定js/ts變量(在scss和less中使用方式)
v-bind是Vue.js中的一個核心指令,用于在Vue組件或DOM元素上綁定數(shù)據(jù)屬性,下面這篇文章主要給大家介紹了關(guān)于Vue3在css中使用v-bind綁定js/ts變量的相關(guān)資料,也可以在scss和less中使用方式,需要的朋友可以參考下2024-04-04
vue封裝自定義指令之動態(tài)顯示title操作(溢出顯示,不溢出不顯示)
這篇文章主要介紹了vue封裝自定義指令之動態(tài)顯示title操作(溢出顯示,不溢出不顯示),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue源碼學(xué)習(xí)之關(guān)于對Array的數(shù)據(jù)偵聽實(shí)現(xiàn)
這篇文章主要介紹了Vue源碼學(xué)習(xí)之關(guān)于對Array的數(shù)據(jù)偵聽實(shí)現(xiàn),Vue使用了一個方式來實(shí)現(xiàn)Array類型的監(jiān)測就是攔截器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04
Vue+ElementUi實(shí)現(xiàn)點(diǎn)擊表格鏈接頁面跳轉(zhuǎn)和路由效果
這篇文章主要介紹了Vue+ElementUi實(shí)現(xiàn)點(diǎn)擊表格中鏈接進(jìn)行頁面跳轉(zhuǎn)和路由,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03

