vue中的localStorage使用方法詳解
在 Vue 項(xiàng)目里能夠直接使用 localStorage,因?yàn)?localStorage 是瀏覽器提供的 Web Storage API 的一部分,它獨(dú)立于 JavaScript 框架,所以可以在 Vue 項(xiàng)目的任何地方使用,包括組件的模板、script 標(biāo)簽內(nèi)部,無(wú)論是 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ù)' };
// 將對(duì)象轉(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)換為對(duì)象
this.data = JSON.parse(jsonData);
console.log('從 localStorage 讀取到數(shù)據(jù):', this.data);
} else {
console.log('localStorage 中沒(méi)有找到對(duì)應(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 只能存儲(chǔ)字符串類(lèi)型的數(shù)據(jù),所以在保存對(duì)象或數(shù)組時(shí),需要先使用 JSON.stringify() 方法將其轉(zhuǎn)換為 JSON 字符串,讀取時(shí)再使用 JSON.parse() 方法將其轉(zhuǎn)換回對(duì)象或數(shù)組。
localStorage 存儲(chǔ)的數(shù)據(jù)會(huì)一直保留在瀏覽器中,除非手動(dòng)刪除,并且存儲(chǔ)大小通常限制在 5MB 左右。
在使用 localStorage 時(shí),要注意數(shù)據(jù)的安全性,避免存儲(chǔ)敏感信息
到此這篇關(guān)于vue里localStorage可以直接用嗎的文章就介紹到這了,更多相關(guān)vue localStorage內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(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)行儲(chǔ)存數(shù)據(jù)
- vue使用localStorage保存登錄信息 適用于移動(dòng)端、PC端
- Vue使用localStorage存儲(chǔ)數(shù)據(jù)的方法
- Vue項(xiàng)目使用localStorage+Vuex保存用戶(hù)登錄信息
- 詳解vue中l(wèi)ocalStorage的使用方法
- 詳解Vue中l(wèi)ocalstorage和sessionstorage的使用
相關(guān)文章
Vue2.0利用vue-resource上傳文件到七牛的實(shí)例代碼
本篇文章主要介紹了Vue2.0利用vue-resource上傳文件到七牛的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
如何利用vue實(shí)現(xiàn)css過(guò)渡和動(dòng)畫(huà)
過(guò)渡Vue在插入、更新或者移除 DOM 時(shí),提供多種不同方式的應(yīng)用過(guò)渡效果這篇文章主要給大家介紹了關(guān)于如何利用vue實(shí)現(xiàn)css過(guò)渡和動(dòng)畫(huà)的相關(guān)資料,需要的朋友可以參考下2021-11-11
vue.js樹(shù)形組件之刪除雙擊增加分支實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家講解vue.js樹(shù)形組件之刪除雙擊增加分支功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-02-02
vue中調(diào)用HTTP請(qǐng)求的詳細(xì)步驟
這篇文章主要介紹了vue中調(diào)用HTTP請(qǐng)求的詳細(xì)步驟,文中通過(guò)代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定幫助,需要的朋友可以參考下2024-07-07
Vue組件教程之Toast(Vue.extend 方式)詳解
這篇文章主要給大家介紹了關(guān)于Vue組件教程之Toast(Vue.extend 方式)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01

