浏览器本地储存

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>localStorage</title>
</head>
<body>
    <h2>localStorage</h2>
    <button onclick="saveData()">点我保存一个数据</button>
    <button onclick="readData()">点我读取一个数据</button>
    <button onclick="deleteData()">点我删除一个数据</button>
    <button onclick="deleteAllData()">点我清空</button>
    <script>
        let p = {name:'张三',age:18}
        console.log(p.toString())
        function saveData () {
            localStorage.setItem('msg','hello!')
            localStorage.setItem('msg2',666)
            localStorage.setItem('person',JSON.stringify(p)) // 把一个对象变成字符串形式,并且能体现出内容
        }
        function readData () {
            console.log(localStorage.getItem('msg2'))
            console.log(localStorage.getItem('msg'))
            const result = localStorage.getItem('person')
            console.log(JSON.parse(result))
            
        }
        function deleteData () {
            localStorage.removeItem('msg2')
            
        }
        function deleteAllData () {
            localStorage.clear()
            
        }
    </script>
</body>
</html>

-----------------------------------------------------------------------------------------

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>sessionStorage</title>
</head>
<body>
    <h2>sessionStorage</h2>
    <button onclick="saveData()">点我保存一个数据</button>
    <button onclick="readData()">点我读取一个数据</button>
    <button onclick="deleteData()">点我删除一个数据</button>
    <button onclick="deleteAllData()">点我清空</button>
    <script>
        let p = {name:'张三',age:18}
        console.log(p.toString())
        function saveData () {
            sessionStorage.setItem('msg','hello!')
            sessionStorage.setItem('msg2',666)
            sessionStorage.setItem('person',JSON.stringify(p)) // 把一个对象变成字符串形式,并且能体现出内容
        }
        function readData () {
            console.log(sessionStorage.getItem('msg2'))
            console.log(sessionStorage.getItem('msg'))
            const result = sessionStorage.getItem('person')
            console.log(JSON.parse(result))
            
        }
        function deleteData () {
            sessionStorage.removeItem('msg2')
                             
        }
        function deleteAllData () {
            sessionStorage.clear()
            
        }
    </script>
</body>
</html>



--------------------------------------------------------------------------------------

  <template>
    <div id="root">
      <div class="todo-container">
        <div class="todo-wrap">
          <MyHeader
          :receive="receive"
          />
          <MyList
          v-bind:todos="todos"
          v-bind:checkTodo="checkTodo"
          v-bind:deleteTodo="deleteTodo"
          />
          <MyFooter
          v-bind:todos="todos"
          v-bind:checkAllTodo="checkAllTodo"
          :clearAllTodo="clearAllTodo"
          ></MyFooter>
        </div>
      </div>
    </div>

    
  </template>
  
  <script>
  // 引入school组件
import MyHeader from './components/MyHeader.vue'
import MyList from './components/MyList.vue'
import MyFooter from './components/MyFooter.vue'

  export default {
    name: 'App',
    components: {
      MyHeader,
      MyList,
      MyFooter,
    },
    data() {
        return {                           // 前项为真的话,能用就用, 不能用就用空数组
            todos:JSON.parse(localStorage.getItem('todos')) || []
        }
    },
    watch: {
      todos: {
        deep:true,
       handler (value) {
        localStorage.setItem('todos',JSON.stringify(value))
       }
      }
    },
    methods :{
      // 添加一个todo
      receive (x) {
        console.log('我是App组件,我收到了数据',x)
        this.todos.unshift(x)
      },
      //勾选or取消勾选一个todo
      checkTodo(id) {
        console.log('@@@@@@@',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 )
        this.todos = this.todos.filter((todo)=>{
           return todo.id !== id
        })

      },
      // 全选或是全不选
      checkAllTodo (done) {
        this.todos.forEach((todo)=>{
          todo.done = done
        })
      },
      // 清除所有已经完成的todo
      clearAllTodo () {
        this.todos = this.todos.filter((todo)=>{
          return !todo.done 
        })
      }
    },
  
  }
  </script>
  
  <style >
  /**base */
  body{
    background: #fff;
  }
  .btn{
    display: inline-block;
    padding: 4px 12px;
    margin-bottom: 0;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),0 1px 2px rgba(0, 0, 0, 0.5);
    border-radius: 4px;
  }
  .btn-danger {
    color:#fff;
    background-color: #da4f49;
    border: 1px solid #bd362f;
  }
  .btn-danger:hover {
    color: #fff;
    background-color: #bd362f;
  }
  .btn:focus {
    outline: none;
  }
  .todo-container {
    width: 600px;
    margin: 0 auto;
  }
  .todo-container .todo-wrap {
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 5px;
  }
  </style>

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值