zoukankan      html  css  js  c++  java
  • 第八课 没有封装localstorage

    <template>


      <div id="app">

          <input type="text" v-model='todo' @keydown="doAdd($event)" />

          <hr>
        <br>

        <h2>进行中</h2>

          <ul>

            <li v-for="(item,key) in list" v-if="!item.checked">
               <input type="checkbox" v-model="item.checked" @change="saveList()" /> {{item.title}}   --  <button @click="removeData(key)">删除</button>
            </li>
          </ul>

        <br>
        <br>
        <h2>已完成</h2>



          <ul>
            <li v-for="(item,key) in list" v-if="item.checked">
              <input type="checkbox"  v-model="item.checked" @change="saveList()" /> {{item.title}}  -- <button @click="removeData(key)">删除</button>
            </li>
          </ul>
      </div>
    </template>

    <script>

        export default {
          data () {
            return {

              todo:'' ,
              list: []
            }
          },
          methods:{
            // var storage={
            //   set(key,value){
            //     localStorage.setItem(key, JSON.stringify(value));
            //   },
            //   get(key){
            //     return JSON.parse(localStorage.getItem(key));
            //   },
            //   remove(key){
            //     localStorage.removeItem(key);
            //   }
            // }
            doAdd(e){
                  // console.log(e);
                  if(e.keyCode==13){
                      this.list.push({
                        title:this.todo,
                        checked:false
                      })
                  }

                  localStorage.setItem('list',JSON.stringify(this.list))
            },
            removeData(key){

                this.list.splice(key,1)

                localStorage.setItem('list', JSON.stringify(this.list))
            }
            , saveList(){
               localStorage.setItem('list', JSON.stringify(this.list))
            }

          },
          mounted(){   /*生命周期函数       vue页面刷新就会触发的方法*/
              // 取出存储的数据,不会丢失本地的数据(重启工程也不会丢)
              var list=JSON.parse(localStorage.getItem('list'));

              if(list){  /*注意判断*/
                this.list=list;
              }
          }

        }
    </script>


    <style lang="scss">

    .finish{


      li{
        background:#eee;
      }
    }

    </style>
  • 相关阅读:
    redis requires Ruby version >= 2.2.2.
    redis-持久化、主从复制、集群
    lucene索引文件大小优化小结
    spring+websocket的整合实例--可使用
    使用nexus3.10搭建maven私有仓库
    oracle 查看所有表的数据量并排序
    Spring框架-经典的案例和demo,一些可以直接用于生产,使用atomikos来处理多数据源的一致性事务等
    ORACLE WITH AS 用法
    判断对象部分属性是否为空
    代码行数统计
  • 原文地址:https://www.cnblogs.com/netflix/p/14626532.html
Copyright © 2011-2022 走看看