• 第九课 代码封装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>


        import storage from './model/storage.js';

        // console.log(storage);

        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
                      })
                  }

                  // 第一个参数:自定义与取值方法对应即可
                  // var list=storage.get('data_block');
                  storage.set('data_block',this.list);
            },
            removeData(key){

                this.list.splice(key,1)

                storage.set('data_block',this.list);
            }
            , saveList(){

               storage.set('data_block',this.list);
            }

          },mounted(){   /*生命周期函数       vue页面刷新就会触发的方法*/

              var list=storage.get('data_block');

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

        }
    </script>


    <style lang="scss">

    .finish{


      li{
        background:#eee;
      }
    }

    </style>
  • 相关阅读:
    HDU.5765.Bonds(DP 高维前缀和)
    SPOJ.TLE
    LOJ.2585.[APIO2018]新家(二分 线段树 堆)
    BZOJ.2679.Balanced Cow Subsets(meet in the middle)
    BZOJ.3293.[CQOI2011]分金币(思路)
    BZOJ.4558.[JLOI2016]方(计数 容斥)
    BZOJ.3631.[JLOI2014]松鼠的新家(树上差分)
    BZOJ.1568.[JSOI2008]Blue Mary开公司(李超线段树)
    BZOJ.1071.[SCOI2007]组队(思路)
    BZOJ.4910.[SDOI2017]苹果树(树形依赖背包 DP 单调队列)
  • 原文地址:https://www.cnblogs.com/netflix/p/14626550.html
Copyright © 2020-2023  润新知