最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue.js實(shí)現(xiàn)備忘錄功能的方法

 更新時(shí)間:2017年07月10日 08:08:09   投稿:jingxian  
下面小編就為大家?guī)硪黄獀ue.js實(shí)現(xiàn)備忘錄功能的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

這個(gè)vue實(shí)現(xiàn)備忘錄的功能demo是K在github上找到的,K覺得這是一個(gè)用來對vue.js入門的一個(gè)非常簡單的demo,所以拿在這里共享一下。

(尊重他人勞動(dòng)成果,從小事做起~  demo原github地址:https://github.com/vuejs/vue)

一、實(shí)現(xiàn)效果

 

二、代碼展示

<!DOCTYPE html>
<html>

  <head>
    <meta charset="UTF-8">
    <title>備忘錄</title>
    <link rel="stylesheet" type="text/css" href="css/index.css" rel="external nofollow" />
    <style>[v-cloak] { display: none; }</style>
  </head>

  <body>
  <section class="todoapp">
   <header class="header">
    <h1>todos</h1>
    <input class="new-todo"
     autofocus autocomplete="off"
     placeholder="What needs to be done?"
     v-model="newTodo"
     @keyup.enter="addTodo">
   </header>
   <section class="main" v-show="todos.length" v-cloak>
    <input class="toggle-all" type="checkbox" v-model="allDone">
    <ul class="todo-list">
     <li v-for="todo in filteredTodos"
      class="todo"
      :key="todo.id"
      :class="{ completed: todo.completed, editing: todo == editedTodo }">
      <div class="view">
       <input class="toggle" type="checkbox" v-model="todo.completed">
       <label @dblclick="editTodo(todo)">{{ todo.title }}</label>
       <button class="destroy" @click="removeTodo(todo)"></button>
      </div>
      <input class="edit" type="text"
       v-model="todo.title"
       v-todo-focus="todo == editedTodo"
       @blur="doneEdit(todo)"
       @keyup.enter="doneEdit(todo)"
       @keyup.esc="cancelEdit(todo)">
     </li>
    </ul>
   </section>
   <footer class="footer" v-show="todos.length" v-cloak>
    <span class="todo-count">
     <strong>{{ todos.length }}</strong> {{ remaining | pluralize }} left
    </span>
    <ul class="filters">
     <li><a href="#/all" rel="external nofollow" :class="{ selected: visibility == 'all' }">All</a></li>
     <li><a href="#/active" rel="external nofollow" :class="{ selected: visibility == 'active' }">Active</a></li>
     <li><a href="#/completed" rel="external nofollow" :class="{ selected: visibility == 'completed' }">Completed</a></li>
    </ul>
    <button class="clear-completed" @click="removeCompleted" v-show="todos.length > remaining">
     Clear completed
    </button>
   </footer>
  </section>
    <footer class="info">
      <p>雙擊編輯一條備忘錄</p>
    </footer>

  </body>

  <script language="JavaScript" src="js/director.js"></script>
  <script language="JavaScript" src="js/vue.js"></script>
  <script language="JavaScript" src="js/index_vue.js"></script>

</html>
// 本地緩存設(shè)置
// 防止頁面關(guān)閉后,數(shù)據(jù)全部丟失的問題
var STORAGE_KEY = 'todos-vuejs-2.0'
var todoStorage = {
  
  // 獲取本地存儲(chǔ)中的內(nèi)容
  fetch:function(){
    // JSON.parse()解析一個(gè)json字符串
    //  localStorage.getItem 從本地存儲(chǔ)中獲取STORAGE_KEY字段的值
    var todos = JSON.parse(localStorage.getItem(STORAGE_KEY)||'[]');
    //  foreach遍歷todos,兩個(gè)參數(shù)分別為遍歷出的每一個(gè)子單元及對應(yīng)的索引
    todos.forEach(function(todo,index){
      todo.id = index;
    })
    todoStorage.uid = todos.length;
    return todos;
  },
  
  // 保存時(shí)將內(nèi)容寫進(jìn)本地存儲(chǔ)
  save:function(todos){
    // localStorage.setItem 將todos轉(zhuǎn)化成字符串存入本地存儲(chǔ),鍵名為STORAGE_KEY
    localStorage.setItem(STORAGE_KEY,JSON.stringify(todos))
  }
  
}

// 可視化狀態(tài)過濾設(shè)置
//  包括全選(all)、選擇未完成(active)、選擇已完成(completed)
var filters = {
  all:function(todos){
    return todos;
  },
  
  //  filter() 方法創(chuàng)建一個(gè)新的數(shù)組,新數(shù)組中的元素是通過檢查指定數(shù)組中符合條件的所有元素。
  active:function(todos){
    return todos.filter(function(todo){
      return !todo.completed;
    })
  },
  
  completed:function(todos){
    return todos.filter(function(todo){
      return todo.completed;
    })
  }
}


// vue實(shí)例化
var app = new Vue({
  
  //  data 參數(shù)設(shè)置
  data:{
    todos:todoStorage.fetch(),
    newTodo:'',
    editedTodo:null,
    visibility:'all'
  },
  
  //  watch 監(jiān)視todos在本地儲(chǔ)存中的變化
  watch:{
    todos:{
      handler:function(todos){
        todoStorage.save(todos)
      },
      deep:true
    }
  },
  
  //  computed 檢測數(shù)據(jù)發(fā)生變動(dòng)時(shí)執(zhí)行函數(shù)
  computed:{
    
    filteredTodos:function(){
      return filters[this.visibility](this.todos)
    },
    
    remaining:function(){
      return filters.active(this.todos).length
    },
    
    allDone:{
      get:function(){
        return this.remaining === 0
      },
      
      set:function(value){
        this.todos.forEach(function(todo){
          todo.completed = value
        })
      }
      
    }
  },
  
  //  methods 方法設(shè)置
  methods:{
    addTodo:function(){
      var value = this.newTodo && this.newTodo.trim()
      if(!value){
        return;
      }
      this.todos.push({
        id:todoStorage.uid++,
        title:value,
        completed:false
      })
      this.newTodo = ''
    },
    
    removeTodo:function(todo){
      this.todos.splice(this.todos.indexOf(todo),1)
    },
    
    editTodo:function(todo){
      this.beforeEditCache = todo.title;
      this.editedTodo = todo
    },
    
    doneEdit:function(todo){
      if(!this.editedTodo){
        return;
      };
      this.editedTodo = null;
      todo.title = todo.title.trim()
      if(!todo.title){
        this.removeTodo(todo)
      }
    },
    
    cancelEdit:function(todo){
      this.editedTodo = null;
      todo.title = this.beforeEditCache
    },
    
    removeCompleted:function(){
      this.todos = filters.active(this.todos)
    }
  },
  
  directives:{
    'todo-focus':function(el,binding){
      if(binding.value){
        el.focus()
      }
    }
  }
})


// hashchange URL的片段標(biāo)識符更改觸發(fā)
function onHashChange(){
  var visbility = window.location.hash.replace(/#\/?/, '');
  if(filters[visbility]){
    app.visibility = visbility
  }else{
    window.location.hash = '';
    app.visbility = 'all'
  }
}

window.addEventListener('hashchange',onHashChange)
onHashChange()

// mount 手動(dòng)掛載
app.$mount('.todoapp')

以上這篇vue.js實(shí)現(xiàn)備忘錄功能的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue在table表中懸浮顯示數(shù)據(jù)及右鍵菜單

    vue在table表中懸浮顯示數(shù)據(jù)及右鍵菜單

    這篇文章主要為大家詳細(xì)介紹了vue在table表中懸浮顯示數(shù)據(jù)及右鍵菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 詳解ESLint在Vue中的使用小結(jié)

    詳解ESLint在Vue中的使用小結(jié)

    ESLint是一個(gè)QA工具,這篇文章主要介紹了詳解ESLint在Vue中的使用小結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-10-10
  • vue實(shí)現(xiàn)分頁功能

    vue實(shí)現(xiàn)分頁功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)分頁功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • VUE 實(shí)現(xiàn)一個(gè)簡易老虎機(jī)的項(xiàng)目實(shí)踐

    VUE 實(shí)現(xiàn)一個(gè)簡易老虎機(jī)的項(xiàng)目實(shí)踐

    老虎機(jī)在很多地方都可以見到,可以設(shè)置中獎(jiǎng)位置,以及中獎(jiǎng)回調(diào),本文主要介紹了VUE 實(shí)現(xiàn)一個(gè)簡易老虎機(jī)的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 前端新手小白的Vue3入坑超詳細(xì)指南

    前端新手小白的Vue3入坑超詳細(xì)指南

    這篇文章主要介紹了如何使用Vite安裝和啟動(dòng)Vue3項(xiàng)目,并推薦了一些常用的第三方庫,如js-tool-big-box、less或sass預(yù)處理器、axios請求庫以及Element?Plus?UI庫,需要的朋友可以參考下
    2024-12-12
  • 深入探討Vue計(jì)算屬性與監(jiān)聽器的區(qū)別和用途

    深入探討Vue計(jì)算屬性與監(jiān)聽器的區(qū)別和用途

    在Vue的開發(fā)中,計(jì)算屬性(Computed Properties)和監(jiān)聽器(Watchers)是兩種非常重要的概念,它們都用于響應(yīng)式地處理數(shù)據(jù)變化,本文將帶你深入了解計(jì)算屬性和監(jiān)聽器的區(qū)別,以及在何時(shí)使用它們,感興趣的朋友可以參考下
    2023-09-09
  • SpringBoot+Vue 前后端合并部署的配置方法

    SpringBoot+Vue 前后端合并部署的配置方法

    這篇文章主要介紹了SpringBoot+Vue 前后端合并部署的配置方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • elementUI vue this.$confirm 和el-dialog 彈出框 移動(dòng) 示例demo

    elementUI vue this.$confirm 和el-dialog 彈出框 移動(dòng) 示例demo

    這篇文章主要介紹了elementUI vue this.$confirm 和el-dialog 彈出框 移動(dòng) 示例demo,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • vue使用element-ui按需引入時(shí)踩過的那些坑

    vue使用element-ui按需引入時(shí)踩過的那些坑

    Element-UI是基于vue實(shí)現(xiàn)的一套不依賴業(yè)務(wù)的UI組件庫,提供了豐富的PC端組件,減少用戶對常用組件的封裝,降低了開發(fā)的難易程度,下面這篇文章主要給大家介紹了關(guān)于vue使用element-ui按需引入時(shí)踩過的那些坑,需要的朋友可以參考下
    2022-05-05
  • vue單頁開發(fā)父子組件傳值思路詳解

    vue單頁開發(fā)父子組件傳值思路詳解

    這篇文章主要介紹了vue單頁開發(fā)父子組件傳值思路詳解,本文是小編抽空整理的思路,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-05-05

最新評論

文成县| 龙泉市| 凉城县| 大兴区| 嵊泗县| 新竹市| 霸州市| 肇东市| 饶平县| 布尔津县| 章丘市| 玉田县| 那坡县| 衡阳市| 墨玉县| 东辽县| 龙门县| 如东县| 海阳市| 浦东新区| 黄平县| 邢台县| 酒泉市| 长寿区| 云阳县| 陆良县| 汝州市| 都安| 赞皇县| 延川县| 西平县| 徐水县| 墨玉县| 恩施市| 报价| 石首市| 赤峰市| 武威市| 宾阳县| 天祝| 宁乡县|