• 本地存储WebStorage


    WebStorage (js 本地存储)

    存储大小一般支持 5MB (不同浏览器可能还不一样)

    浏览器通过 Window.sessionStorage 和 Window.localStorage 属性来实现本地存储机制

    相关API

    • XXXStorage.setItem(‘key’,'value) 该方法接受一个键和值作为参数,会把键值添加到存储中,如果键名存在,则更新其对应的值,
    • xxxStorage.getItem(‘key’) 该方法接受一个键名作为参数,返回键名对应的值。
    • xxxStorge.removeItem(‘key’) 该方法接受一个键名作为参数,并把该键名从存储中删除
    • xxxStorage.clear() 该方法会清空存储中的所有数据。

    备注

    • SessionStorage 存储的内容会随着浏览器的关闭而消息
    • LocalStorage 存储的内容,需要手动清除才会消失

    localStorage:

    <h2>localStorage</h2>
    <button onclick="saveDate()">点我保存数据</button><br/>
    <button onclick="readDate()">点我读数据</button><br/>
    <button onclick="deleteDate()">点我删除数据</button><br/>
    <button onclick="deleteAllDate()">点我清空数据</button><br/>
    
    <script>
      let person = {name:"JOJO",age:20}
    
      function saveDate(){
        localStorage.setItem('msg','localStorage')
        localStorage.setItem('person',JSON.stringify(person))
      }
      function readDate(){
        console.log(localStorage.getItem('msg'))
        const person = localStorage.getItem('person')
        console.log(JSON.parse(person))
      }
      function deleteDate(){
        localStorage.removeItem('msg')
        localStorage.removeItem('person')
      }
      function deleteAllDate(){
        localStorage.clear()
      }
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    sessionStorage

    <h2>sessionStorage</h2>
    <button onclick="saveDate()">点我保存数据</button><br/>
    <button onclick="readDate()">点我读数据</button><br/>
    <button onclick="deleteDate()">点我删除数据</button><br/>
    <button onclick="deleteAllDate()">点我清空数据</button><br/>
    
    <script>
      let person = {name:"JOJO",age:20}
    
      function saveDate(){
        sessionStorage.setItem('msg','sessionStorage')
        sessionStorage.setItem('person',JSON.stringify(person))
      }
      function readDate(){
        console.log(sessionStorage.getItem('msg'))
        const person = sessionStorage.getItem('person')
        console.log(JSON.parse(person))
      }
      function deleteDate(){
        sessionStorage.removeItem('msg')
        sessionStorage.removeItem('person')
      }
      function deleteAllDate(){
        sessionStorage.clear()
      }
    </script>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    使用本地存储优化Todo-list

    <template>
    	<div id="root">
    		<div class="todo-container">
    			<div class="todo-wrap">
    				<MyHeader :addTodo="addTodo"/>
    				<MyList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo"/>
    				<MyFooter :todos="todos" :checkAllTodo="checkAllTodo" :clearAllTodo="clearAllTodo"/>
    			</div>
    		</div>
    	</div>
    </template>
    
    <script>
    	import MyHeader from './components/MyHeader'
    	import MyList from './components/MyList'
    	import MyFooter from './components/MyFooter.vue'
    
    	export default {
    		name:'App',
    		components:{MyHeader,MyList,MyFooter},
    		data() {
    			return {
    				// 🔴从本地存储中获得数据,null就创建空数组[]
    				todos:JSON.parse(localStorage.getItem('todos')) || []
    			}
    		},
    		methods: {
    			//添加一个todo
    			addTodo(todoObj){
    				this.todos.unshift(todoObj)
    			},
    			//勾选or取消勾选一个todo
    			checkTodo(id){
    				this.todos.forEach((todo)=>{
    					if(todo.id === id) todo.done = !todo.done
    				})
    			},
    			//删除一个todo
    			deleteTodo(id){
    				this.todos = this.todos.filter( todo => todo.id !== id )
    			},
    			//全选or取消全选
    			checkAllTodo(done){
    				this.todos.forEach((todo)=>{
    					todo.done = done
    				})
    			},
    			//清除所有已经完成的todo
    			clearAllTodo(){
    				this.todos = this.todos.filter((todo)=>{
    					return !todo.done
    				})
    			}
    		},
        // 🔴数据发生改变就放到本地存储中,注意深度侦听,以及JSON转化为字符串
    		watch: {
    			todos:{
    				deep:true,
    				handler(value){
    					localStorage.setItem('todos',JSON.stringify(value))
    				}
    			}
    		},
    	}
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
  • 相关阅读:
    git本地项目与远程仓库建立连接
    Java 多线程(七):线程池
    【数据结构-队列】队列介绍
    Activiti7笔记
    希尔排序算法(代码实现) [数据结构][Java]
    net-java-php-python-单位办公OA系统计算机毕业设计程序
    ZooKeeper 之zkCli.sh 客户端一文读懂
    springboot如何整个Swagger呢?
    【必会】Kafka基本概念(topic、partition、offset、broker、生产者、消费者、消费者组等)【知识点速记速查】
    jquery监听滚动条,实现“返回顶部”
  • 原文地址:https://blog.csdn.net/fd2025/article/details/125430026