最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)

 更新時(shí)間:2021年04月19日 14:14:31   作者:沙漏在下雨  
這篇文章主要介紹了Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文主要介紹了Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn),分享給大家,具體如下:

在這里插入圖片描述

使用Vue封裝一個(gè)簡易的Todolist的小案例. 同時(shí)加入了瀏覽器本地緩存的技術(shù)手段.

瀏覽器本地緩沖:

  • 前提: 一般我們定義的變量,或者用Vuex保存的數(shù)據(jù), 當(dāng)瀏覽器進(jìn)行了一個(gè)刷新 那么這個(gè)數(shù)據(jù)就會丟失, 這樣就做不出歷史記錄的效果了, 但是, 使用瀏覽器緩存就可以幫助我們解決這個(gè)問題…
  • 瀏覽器緩存分為二種 sessionStorage 和 localStorage, 二種原型鏈分別如下:

在這里插入圖片描述

在這里插入圖片描述

可以看得出, 他們的原型鏈上基本都是一樣的, 唯一的區(qū)別在于,

  • localStorage 作用于本地緩存, 時(shí)間是持久的,除非手動去刪除, 或者清空, 不然一直都存在瀏覽器中
  • sessionStorage 作用與會話緩存, 生命周期只存在于本次打開瀏覽器會話, 當(dāng)完成的關(guān)閉瀏覽器,那么信息就會丟失, 而僅僅刷新頁面, 數(shù)據(jù)仍然保存。

本次實(shí)例,使用的是 sessionStorage, 并對此進(jìn)行了一次小封裝.

const  storage = {
	set(key, value){
		window.sessionStorage.setItem(key, JSON.stringify(value));
	},
	get(key){
		return JSON.parse(window.sessionStorage.getItem(key));
	},
	remove(key){
		window.sessionStorage.removeItem(key);
	}
}

export default storage;

實(shí)例代碼:

<template>
	<div class="todo">
		<header>
			<input type="text" placeholder="輸入..." v-model="keyword" @keydown.enter="handleList">
			TodoList
		</header>
		<!-- 正在進(jìn)行 -->
		<h4>正在進(jìn)行...{{dolistNumber}}</h4>
		<template v-for="(item, index) in dolist" :key="index">
			<div class="dolist" v-if="!item.checked">
				<label :for="index +'l'">
					<input type="checkbox" v-model="item.checked" :id="index +'l'" @change="handleChecked">
					{{item.title}}
				</label>
				<span @click="cancalDo(index)">X</span>
			</div>
		</template>

		<!-- 已經(jīng)完成 -->
		<h4>已經(jīng)完成...{{dolist.length - dolistNumber}}</h4>
		<template v-for="(item, index) in dolist" :key="index">
			<div class="dolist" v-if="item.checked">
				<label :for="index +'ll'">
					<input type="checkbox" v-model="item.checked" :id="index +'ll'"  @change="handleChecked">
					{{item.title}}
				</label>
				<span @click="cancalDo(index)">X</span>
			</div>
		</template>
	</div>
</template>

<script>
	import storage from '../storage.js';
	export default {
		name: "todoList",
		data() {
			return {
				keyword: "", //  輸入的選項(xiàng)
				dolist: [],
			}
		},
		computed:{
			dolistNumber(){
				return this.dolist.filter(item => item.checked === false).length;
			}
		},
		methods: {
			handleChecked(){
				//  當(dāng)更改狀態(tài)之后 重新刷新
				storage.set('dolist', this.dolist);
			},
			handleList() {
				if (this.keyword !== "") {
					this.dolist.push({
						title: this.keyword,
						checked: false,
					});
					this.keyword = "";
					storage.set('dolist', this.dolist);
				}
				
			},
			cancalDo(index) {
				// 刪除這個(gè)
				this.dolist.splice(index, 1);
				storage.set('dolist', this.dolist);
			}
		},
		mounted(){
			let dolist = storage.get('dolist');
			if(dolist){
				this.dolist = dolist;
			}
		},

	}	
</script>

<style>
	.todo {
		margin: 400px auto;
		min-height: 300px;
		width: 800px;
		background-color: #eee;
	}

	.todo header {
		position: relative;
		text-align: center;
		height: 60px;
		line-height: 60px;
		font-size: 20px;
		border-bottom: 2px solid #fff;
	}

	.todo header input {
		position: absolute;
		left: 40px;
		top: 50%;
		transform: translateY(-50%);

		outline: none;
		line-height: 30px;
		border-radius: 15px;
		padding-left: 30px;
		border: 1px solid #999;
		font-size: 16px;
		width: 100px;
		transition: all .6s linear;
	}

	.todo header input:focus {
		width: 200px;
	}

	.dolist {
		padding: 20px;
		font-size: 16px;

	}

	.dolist label {
		cursor: pointer;
	}

	.dolist input {
		margin-right: 10px;

	}

	.dolist span:last-child {
		float: right;
		border: 1px solid gray;
		background-color: #999;
		color: #fff;
		border-radius: 50%;
		padding: 5px;
	}

	h4 {
		padding-bottom: 20px;
		text-align: center;
	}
</style>

到此這篇關(guān)于Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue TodoList內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue3中refs用法舉例小結(jié)

    vue3中refs用法舉例小結(jié)

    這篇文章主要給大家介紹了關(guān)于vue3中refs用法舉例的相關(guān)資料,Vue.js中的$refs是一個(gè)對象,它持有已注冊過ref特性的所有組件和元素,需要的朋友可以參考下
    2023-08-08
  • Vue中使用js制作進(jìn)度條式數(shù)據(jù)對比動畫

    Vue中使用js制作進(jìn)度條式數(shù)據(jù)對比動畫

    這篇文章主要為大家詳細(xì)介紹了Vue中使用js制作進(jìn)度條式數(shù)據(jù)對比動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • vue實(shí)現(xiàn)點(diǎn)擊某個(gè)div顯示與隱藏內(nèi)容功能實(shí)例

    vue實(shí)現(xiàn)點(diǎn)擊某個(gè)div顯示與隱藏內(nèi)容功能實(shí)例

    最近做項(xiàng)目有用到某個(gè)div顯示與隱藏內(nèi)容,所以下面這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)點(diǎn)擊某個(gè)div顯示與隱藏內(nèi)容功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • 淺析Vue3中Excel下載模板并導(dǎo)入數(shù)據(jù)功能的實(shí)現(xiàn)

    淺析Vue3中Excel下載模板并導(dǎo)入數(shù)據(jù)功能的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Vue3中的Excel數(shù)據(jù)管理,即下載模板并導(dǎo)入數(shù)據(jù)功能的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2024-05-05
  • 關(guān)于Pinia狀態(tài)持久化問題

    關(guān)于Pinia狀態(tài)持久化問題

    這篇文章主要介紹了關(guān)于Pinia狀態(tài)持久化問題,具有很好的參考價(jià)值,希望對大家有所幫助。
    2023-03-03
  • Vue鼠標(biāo)點(diǎn)擊事件和鍵盤事件舉例詳解

    Vue鼠標(biāo)點(diǎn)擊事件和鍵盤事件舉例詳解

    在Vue框架中我們經(jīng)常需要綁定各種JS事件,如"點(diǎn)擊事件"、"鼠標(biāo)移動事件"、"鍵盤事件"等等,這篇文章主要給大家介紹了關(guān)于Vue鼠標(biāo)點(diǎn)擊事件和鍵盤事件的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • vue中使用[provide/inject]實(shí)現(xiàn)頁面reload的方法

    vue中使用[provide/inject]實(shí)現(xiàn)頁面reload的方法

    這篇文章主要介紹了在vue中使用[provide/inject]實(shí)現(xiàn)頁面reload的方法,文中給大家提到了在vue中實(shí)現(xiàn)頁面刷新不同的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Vue組件解析之如何自定義彈窗組件ByDialog

    Vue組件解析之如何自定義彈窗組件ByDialog

    這篇文章主要介紹了Vue組件解析之如何自定義彈窗組件ByDialog問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Vue自定義組件中v-model的使用方法示例

    Vue自定義組件中v-model的使用方法示例

    日常開發(fā)中除了直接在input標(biāo)簽上使用v-model指令外,封裝的組件也需要v-model,下面這篇文章主要給大家介紹了關(guān)于Vue自定義組件中v-model使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Vscode如何創(chuàng)建vue項(xiàng)目

    Vscode如何創(chuàng)建vue項(xiàng)目

    這篇文章主要介紹了Vscode如何創(chuàng)建vue項(xiàng)目問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04

最新評論

双江| 江西省| 冷水江市| 新晃| 都兰县| 宜阳县| 蓝田县| 珲春市| 墨脱县| 凤台县| 宁化县| 沂南县| 南充市| 济阳县| 嘉定区| 三河市| 应用必备| 兴海县| 容城县| 库车县| 榆林市| 贵阳市| 长岭县| 隆子县| 丰都县| 星座| 岚皋县| 松潘县| 漳州市| 太谷县| 盐源县| 永昌县| 牙克石市| 竹溪县| 独山县| 靖安县| 会泽县| 彩票| 吕梁市| 阳谷县| 仁化县|