• vue中使用localstorage


    1、store.js(读取写入到localstorage)

    const STORAGE_KEY="todos-vuejs"
    export default{
        fetch(){
            return JSON.parse(window.localStorage.getItem(
                STORAGE_KEY||"[]"))
        },
        save(items){
            window.localStorage.setItem(STORAGE_KEY,JSON.stringify(items))
        }
    }

    2、App.vue

    <template>
      <div id="app">
        <h1>{{title}}</h1>
        <h1 v-text="title"></h1>
        <h1 v-html="title"></h1>
        <input type="text" v-model="newItem" v-on:keyup.enter="addNew">
        <ul>
            <li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click="toggleFinish(item)">
            {{item.label}}
            </li>
        </ul>
    
      </div>
    </template>
    
    <script>
    import Store from './Store'
    //console.log(Store)
    export default {
      
       data () {
        return {
          title: '<span>?</span>this is a todolist',
          items:Store.fetch(),
          // items:[
          //     {
          //       label:'coding',
          //       isFinished:false
      
          //     },
          //     {
          //       label:'walking',
          //       isFinished:true
      
          //     }
          // ],
          newItem:''
        }
      },
      watch:{
        items:{
          handler:function(items){
            // console.log(val,oldVal)
             // console.log(items);
    
            Store.save(items);
             // console.log(items);
    
          }
        },
        deep:true//也检测items内部的key的变化
      },
      methods:{
        toggleFinish:function(item){
          // console.log(item);
          item.isFinished=!item.isFinished;
          console.log(this.items);
        },
        addNew:function(){
          // this.newItem;
          // console.log(this.newItem);
          this.items.push({
            label:this.newItem,
            isFinished:false
          })
          this.newItem='';
        }
      }
    }
    </script>
    
    <style>
    .finished{
      text-decoration:underline;
    }
    #app {
      font-family: 'Avenir', Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    </style>
  • 相关阅读:
    ACM 2的N次方
    文件默认打开方式 转
    java 的 一点记录
    zhuan 漫谈C语言及如何学习C语言
    eclipse
    code::blocks
    心态决定命运_no excuses, suck it up, obey your teacher
    uml_2_application and viso application
    paint conflict with lingoes
    stm learning record
  • 原文地址:https://www.cnblogs.com/hongmaju/p/6843311.html
Copyright © 2020-2023  润新知