vue2的todolist入門小項(xiàng)目的詳細(xì)解析
看完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
- 安裝谷歌插件vue.js devtools
- 下載vue.js的webpack模板
- 下載todomvc的模板 (提供html和css)(也可以直接$ git clone https://github.com/tastejs/todomvc-app-template.git 來下載)
- 把todomvc的index.html拖到todolist文件夾去覆蓋里面的index.html
- 打開todomvc的json文件,會(huì)看到 “todomvc-app-css”: “^2.0.0”,就是要你 npm install todomvc-app-css -S 從而下載該css
- 刪點(diǎn)todolsit index.html的默認(rèn)css,js引用,src文件夾下的main.js引入模板css(import‘todomvc-app-css/index.css')
- 引入vue(import Vue form ‘vue')
- 等寫完代碼后 $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'
//添加localStorage
var 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í)有所幫助,也希望大家多多支持腳本之家。
- Vue中父子組件通訊之todolist組件功能開發(fā)
- Vue從TodoList中學(xué)父子組件通信
- 詳解Vue的computed(計(jì)算屬性)使用實(shí)例之TodoList
- vue實(shí)現(xiàn)ToDoList簡(jiǎn)單實(shí)例
- 利用vue寫todolist單頁應(yīng)用
- vue組件編寫之todolist組件實(shí)例詳解
- 使用Vue完成一個(gè)簡(jiǎn)單的todolist的方法
- Vue.js實(shí)現(xiàn)簡(jiǎn)單ToDoList 前期準(zhǔn)備(一)
- vue實(shí)現(xiàn)留言板todolist功能
- 使用Vue父子組件通信實(shí)現(xiàn)todolist的功能示例代碼
相關(guān)文章
vue?navbar?tabbar導(dǎo)航條根據(jù)位置移動(dòng)實(shí)現(xiàn)定位、顏色過渡動(dòng)畫效果的代碼
這篇文章主要介紹了vue?navbar?tabbar導(dǎo)航條根據(jù)位置移動(dòng)實(shí)現(xiàn)定位、顏色過渡動(dòng)畫效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Vue項(xiàng)目打包部署到apache服務(wù)器的方法步驟
這篇文章主要介紹了Vue項(xiàng)目打包部署到apache服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
ant-design-vue 實(shí)現(xiàn)表格內(nèi)部字段驗(yàn)證功能
這篇文章主要介紹了ant-design-vue 實(shí)現(xiàn)表格內(nèi)部字段驗(yàn)證功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
vue 項(xiàng)目中使用websocket的正確姿勢(shì)
這篇文章主要介紹了vue 項(xiàng)目中使用websocket的實(shí)例代碼,通過實(shí)例代碼給大家介紹了在utils下新建websocket.js文件的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
Vue 通過自定義指令回顧v-內(nèi)置指令(小結(jié))
這篇文章主要介紹了Vue 通過自定義指令回顧v-內(nèi)置指令(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
Vue中以HTML形式顯示內(nèi)容并動(dòng)態(tài)生成HTML代碼的方法
有的時(shí)候我們想在vue中直接顯示一個(gè)html的網(wǎng)頁,如果用富文本方式,那么內(nèi)容就會(huì)太多,那么怎么處理呢?這篇文章主要給大家介紹了關(guān)于Vue中如何以HTML形式顯示內(nèi)容并動(dòng)態(tài)生成HTML代碼的相關(guān)資料,需要的朋友可以參考下2024-03-03
記錄--使用el-time-picker默認(rèn)值遇到的問題
這篇文章主要介紹了記錄--使用el-time-picker默認(rèn)值遇到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
IOS上微信小程序密碼框光標(biāo)離開提示存儲(chǔ)密碼的完美解決方案
ios密碼框輸入密碼光標(biāo)離開之后會(huì)提示存儲(chǔ)密碼的彈窗,關(guān)于這樣的問題怎么解決呢,下面給大家分享IOS上微信小程序密碼框光標(biāo)離開提示存儲(chǔ)密碼的完美解決方案,感興趣的朋友一起看看吧2024-07-07

