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

Solution for FileZilla 425 Unable to connect to FTP (Alibaba Cloud Server)

Alibaba Cloud Server cannot connect to FTP FileZi...

After docker run, the status is always Exited

add -it docker run -it -name test -d nginx:latest...

Analysis and description of network configuration files under Ubuntu system

I encountered a strange network problem today. I ...

Ubuntu 16.04 installation tutorial under VMware 12

This article shares with you the installation tut...

Detailed explanation of samba + OPENldap to build a file sharing server

Here I use samba (file sharing service) v4.9.1 + ...

Ubuntu 20.04 sets a static IP address (including different versions)

Because Ubuntu 20.04 manages the network through ...

HTML code text box limit input text box becomes gray limit text box input

Method 1: Set the readonly attribute to true. INPU...

Implementing a simple web clock with JavaScript

Use JavaScript to implement a web page clock. The...

Example of using Dockerfile to build an nginx image

Introduction to Dockerfile Docker can automatical...

Common usage of hook in react

Table of contents 1. What is a hook? 2. Why does ...

How to install docker and portainer in kali

With the emergence of docker, many services have ...

CentOS 7 Forgot Password Solution Process Diagram

need Whether it is a Windows system or a Linux sy...

A brief analysis of the basic implementation of Vue detection data changes

Table of contents 1. Object change detection 2. Q...

JavaScript to achieve dynamic table effect

This article shares the specific code for JavaScr...

Summary of the unknown usage of "!" in Linux

Preface In fact, the humble "!" has man...