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

Simple example of HTML text formatting (detailed explanation)

1. Text formatting: This example demonstrates how...

HTML Tutorial: Collection of commonly used HTML tags (6)

These introduced HTML tags do not necessarily ful...

Analysis of slow insert cases caused by large transactions in MySQL

【question】 The INSERT statement is one of the mos...

Detailed description of the life cycle of React components

Table of contents 1. What is the life cycle 2. Lo...

Detailed explanation of the use of MySQL Online DDL

Table of contents text LOCK parameter ALGORITHM p...

Basic commands for MySQL database operations

1. Create a database: create data data _name; Two...

MYSQL slow query and log settings and testing

1. Introduction By enabling the slow query log, M...

mysql5.7.20 installation and configuration method graphic tutorial (mac)

MySQL 5.7.20 installation and configuration metho...

Win2008 R2 mysql 5.5 zip format mysql installation and configuration

Win2008 R2 zip format mysql installation and conf...

MySQL-group-replication configuration steps (recommended)

MySQL-Group-Replication is a new feature develope...

Common causes and solutions for slow MySQL SQL statements

1. Slow query due to lack of index or invalid ind...

How to completely uninstall mysql under CentOS

This article records the complete uninstallation ...

Detailed explanation of some settings for Table adaptation and overflow

1. Two properties of table reset: ①border-collaps...

How does the MySQL database implement the XA specification?

MySQL consistency log What happens to uncommitted...