国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vue2的todolist入門小項(xiàng)目的詳細(xì)解析

2019-11-19 16:36:45
字體:
供稿:網(wǎng)友

看完vue2的官方文檔后,找個(gè)入門項(xiàng)目鞏固下知識(shí)點(diǎn),簡(jiǎn)單的todolsit是個(gè)不錯(cuò)的選擇。

項(xiàng)目用到了vue.js vue.cli webpack ES6 node環(huán)境,完成項(xiàng)目后會(huì)對(duì)這些技術(shù)棧有了些了解。

準(zhǔn)備開發(fā)環(huán)境

$ npm install -g vue-cli$ vue init ,比如 vue init webpack todolist$ cd todolist$ npm install$ npm run dev
  1. 安裝谷歌插件vue.js devtools
  2. 下載vue.js的webpack模板
  3. 下載todomvc的模板 (提供html和css)(也可以直接$ git clone https://github.com/tastejs/todomvc-app-template.git 來下載)
  4. 把todomvc的index.html拖到todolist文件夾去覆蓋里面的index.html
  5. 打開todomvc的json文件,會(huì)看到 “todomvc-app-css”: “^2.0.0”,就是要你 npm install todomvc-app-css -S 從而下載該css
  6. 刪點(diǎn)todolsit index.html的默認(rèn)css,js引用,src文件夾下的main.js引入模板css(import‘todomvc-app-css/index.css')
  7. 引入vue(import Vue form ‘vue')
  8. 等寫完代碼后 $npm run build 一鍵打包構(gòu)建,會(huì)看到dist文件夾

main.js的代碼

//后面的為注釋講解, ~表示串聯(lián)index.html的對(duì)應(yīng)內(nèi)容import 'todomvc-app-css/index.css'import Vue from 'vue'//添加localStoragevar STORAGE_KEY = 'todos-vuejs-2.0'var todoStorage = { fetch: function () {  var todos = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')  todos.forEach(function (todo, index) {   todo.id = index  })  todoStorage.uid = todos.length  return todos }, save: function (todos) {  localStorage.setItem(STORAGE_KEY, JSON.stringify(todos)) }}//用過濾器篩選出三種狀態(tài)var filters = { all(todos) {  return todos }, active(todos) {  return todos.filter((todo) => {   return !todo.completed  }) }, completed(todos) {  return todos.filter((todo) => {   return todo.completed  }) },}let app = new Vue({ el: '.todoapp',  // ~ <section class="todoapp"> data: {  msg: 'hello world',  title: '待做清單',  // 渲染標(biāo)題 ~ {{title}}  newTodo: '',   todos: todoStorage.fetch(), // ~ v-show="todos.length" ; ~ {{todos.length>1?'items':'item'}} 渲染 li ~ v-for="(todo,index) in filteredTodos"   editedTodo: '',  // 空的編輯對(duì)象  hashName: 'all'   },  watch: {  todos: {   handler: function (todos) {    todoStorage.save(todos)   },   deep: true  } },  computed: {  remain() {   return filters.active(this.todos).length  //未完成事項(xiàng)的數(shù)量 ~ {{remain}}  },   isAll: {   // ~ v-model="isAll"   get() {    return this.remain === 0   },   set(value) {    this.todos.forEach((todo) => {     todo.completed = value    })   }  },  filteredTodos() {  //用hashName過濾出當(dāng)前頁面的todos ~ v-for="(todo,index) in filteredTodos"    return filters[this.hashName](this.todos)  } }, methods: {  addTodo(e) { //輸入值為空時(shí),不添加(trim去除前后空格) ~ v-model.trim="newTodo"    if (!this.newTodo) {    return   }   this.todos.push({    id: todoStorage.uid++,    content: this.newTodo,    completed: false //結(jié)合v-model 根據(jù)completed狀態(tài)綁定樣式 ~:class="{completed:todo.completed; ~ v-model="todo.completed"   })   this.newTodo = ''  },  removeTodo(index) {  //綁定x樣式,點(diǎn)擊刪除該todo ~ @click="removeTodo(index)"   this.todos.splice(index, 1)  },  editTodo(todo) {     //編輯 ~ @dblclick="editTodo(todo)"   this.editCache = todo.content //儲(chǔ)存編輯前的內(nèi)容   this.editedTodo = todo // 點(diǎn)擊編輯里面的內(nèi)容而不是只是空文本框~ editing:todo==editedTodo}"  },  doneEdit(todo, index) { //失去焦點(diǎn)后 ~ @blur="doneEdit(todo)";@keyup.enter="doneEdit(todo)"   this.editedTodo = null //不存在編輯了或者說編輯已完成   if (!todo.content) { //如果編輯后沒有內(nèi)容了,刪除該todo    this.removeTodo(index)   }  },  cancelEdit(todo) {  //按esc鍵取消此次編輯操作 ~ @keyup.esc="cancelEdit(todo)">   this.editedTodo = null      todo.content = this.editCache //當(dāng)esc取消編輯時(shí),還原編輯前的內(nèi)容  },  clear() { //點(diǎn)擊清除已完成的功能 ~ @click="clear"   this.todos = filters.active(this.todos) //獲取并渲染未完成的事項(xiàng) ~   } }, directives: {  //自定義屬性 ~ v-focus="todo == editedTodo"  focus(el, value) { //文本框雙擊獲取焦點(diǎn)   if (value) {    el.focus()   }  } }})//hash(url地址中#以及之后的字符)function hashChange() { // ~ :class="{selected:hashName=='all'}";:class="{selected:hashName=='active'}";:class="{selected:hashName=='completed'}" let hashName = location.hash.replace(/#//?/, '') //正則表達(dá)式去除#/?,獲取如all,active,completed if (filters[hashName]) {  //如果過濾狀態(tài)的hashName存在  app.hashName = hashName //給整個(gè)app變量里的hashName賦上那個(gè)值 } else {  location.hash = ''  //取消  app.hashName = 'all' //否則就賦值‘a(chǎn)ll',回到全部事項(xiàng)的頁面 }}window.addEventListener('hashchange', hashChange) //全局監(jiān)聽hash

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 惠来县| 佛冈县| 抚顺市| 公安县| 平度市| 郁南县| 常山县| 滨州市| 固阳县| 边坝县| 阜新市| 远安县| 略阳县| 奈曼旗| 兰溪市| 全州县| 临猗县| 章丘市| 钟山县| 类乌齐县| 边坝县| 镇康县| 清水县| 和顺县| 徐水县| 西安市| 新邵县| 革吉县| 石首市| 潞城市| 梧州市| 兴安盟| 区。| 盐城市| 资兴市| 昌宁县| 巴中市| 马龙县| 钦州市| 福清市| 财经|