Vue encapsulates a TodoList example and implements the application of browser local cache

Vue encapsulates a TodoList example and implements the application of browser local cache

This article mainly introduces the case of Vue encapsulating a TodoList and the application implementation of browser local cache, and shares it with you, as follows:

insert image description here

A simple Todolist example is encapsulated using Vue. The browser local cache technology is also added.

Browser local cache:

  • Premise : Generally, the variables we define, or the data saved by Vuex, will be lost when the browser is refreshed, so the historical record effect cannot be achieved. However, using browser cache can help us solve this problem...
  • Browser cache is divided into two types: sessionStorage and localStorage. The prototype chains of the two are as follows:

insert image description here

insert image description here

It can be seen that their prototype chains are basically the same, the only difference is that

  • localStorage acts as a local cache, which is persistent. Unless you delete it manually or clear it, it will always exist in the browser.
  • sessionStorage is used for session caching. Its life cycle only exists in the current browser session. When the browser is closed, the information will be lost. However, if you only refresh the page, the data will still be saved.

In this example, sessionStorage is used and a small package is made for it.

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;

Example code:

<template>
	<div class="todo">
		<header>
			<input type="text" placeholder="Enter..." v-model="keyword" @keydown.enter="handleList">
			TodoList
		</header>
		<!-- In Progress -->
		<h4>In progress...{{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>

		<!-- Completed -->
		<h4>Completed...{{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: "", // input options dolist: [],
			}
		},
		computed:{
			dolistNumber(){
				return this.dolist.filter(item => item.checked === false).length;
			}
		},
		methods: {
			handleChecked(){
				// Refresh after changing the status 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) {
				// delete this 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>

This concludes this article about the case of Vue encapsulating a TodoList and the application implementation of browser local cache. For more relevant Vue TodoList content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the simple todolist example of vuex
  • Vue learns parent-child component communication from TodoList
  • Vuejs implements simple todoList function and component example code
  • vue.js example todoList project
  • Detailed explanation of Vue's TodoList case

<<:  Detailed explanation of using top command to analyze Linux system performance

>>:  Brief analysis of the MySQL character set causing database recovery errors

Recommend

Xftp download and installation tutorial (graphic tutorial)

If you want to transfer files between Windows and...

ElementUI implements sample code for drop-down options and multiple-select boxes

Table of contents Drop-down multiple-select box U...

Comprehensive website assessment solution

<br />Sometimes you may be asked questions l...

A brief talk about cloning JavaScript

Table of contents 1. Shallow cloning 2. Deep clon...

Summary of various methods for Vue to achieve dynamic styles

Table of contents 1. Ternary operator judgment 2....

Detailed explanation of how Nginx works

How Nginx works Nginx consists of a core and modu...

mysql update case update field value is not fixed operation

When processing batch updates of certain data, if...

Is a design that complies with design specifications a good design?

In the past few years of my career, I have writte...

The whole process record of vue3 recursive component encapsulation

Table of contents Preface 1. Recursive components...

jQuery plugin to achieve carousel effect

A jQuery plugin every day - jQuery plugin to impl...

Detailed explanation of the marquee attribute in HTML

This tag is not part of HTML3.2 and is only suppo...