這篇文章主要為大家展示了“Vue如何制作Todo List網(wǎng)頁(yè)”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Vue如何制作Todo List網(wǎng)頁(yè)”這篇文章吧。
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),赤坎企業(yè)網(wǎng)站建設(shè),赤坎品牌網(wǎng)站建設(shè),網(wǎng)站定制,赤坎網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,赤坎網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
具體內(nèi)容如下
跟著老師學(xué)習(xí)Vue,我喜歡清爽的界面,就自己改了樣式,看起來(lái)還不錯(cuò),以后就用來(lái)記錄要做的事情,實(shí)用性還蠻強(qiáng)。
Vue非常容易上手,運(yùn)用到了雙向綁定機(jī)制,即HTML里的DOM元素與JS里的Vue實(shí)例進(jìn)行雙向綁定,只要Vue實(shí)例中的代理數(shù)據(jù)改變,HTML中的實(shí)際數(shù)據(jù)就會(huì)跟著變,這和原生的Js的命令驅(qū)動(dòng)模式不同,它是數(shù)據(jù)驅(qū)動(dòng)模式,通過(guò)數(shù)據(jù)的改變來(lái)控制DOM的變化。什么意思呢?我們會(huì)在接下去的學(xué)習(xí)中慢慢實(shí)踐。
Todo List這個(gè)網(wǎng)頁(yè)用到了很多Vue的指令,在這里我貼出一部分代碼,全部代碼請(qǐng)戳我的Github
以下是HTML部分
<div class="main"> <h4 class="big-title">添加任務(wù):</h4> <input placeholder="在此添加任務(wù)" class="task-input" type="text" v-model="things" @keyup.enter="addTodo" /> <ul class="task-count" v-show="list.length"> <li> {{unCheckedLength}}個(gè)任務(wù)未完成</li> <li class="action"> <a :class="{active: visibility == 'all'}" href="#all" rel="external nofollow" >所有任務(wù)</a> <a :class="{active: visibility == 'unfinished'}"href="#unfinished" rel="external nofollow" >未完成任務(wù)</a> <a :class="{active: visibility == 'finished'}"href="#finished" rel="external nofollow" >完成任務(wù)</a> </li> </ul> <div class="tasks"> <span class="no-task-tip" v-show="!list.length">還沒(méi)有添加任何任務(wù)</span> <ul class="todo-list"> <li class="todo" v-for="item in filteredList" :class="{completed: item.isChecked,editing:item === editItem}" > <div class="view"> <div class="word"> <input class="toggle" type="checkbox" v-model="item.isChecked" > <label @dblclick="editTodo(item)">{{item.title}}</label> </div> <button class="destroy" type="text" @click="deleteTodo(item)">×</button> </div> <input v-focus="editItem === item" class="edit" type="text" v-model="item.title" @blur="edited" @keyup.enter="edited" @keyup.esc="cancel(item)" /> </li> </ul> </div> </div>
Vue實(shí)例部分
var vm = new Vue({ el: ".main", data: { list:list, things:"", editItem:"", beforeTitle:"", visibility:"all", inputId:"", }, watch:{ list:{ handler:function(){ store.save("todolist",this.list) }, deep:true } }, computed:{ unCheckedLength(){ return this.list.filter(function(item){ return item.isChecked == false }).length }, filteredList(){ return filter[this.visibility] ? filter[this.visibility](this.list) : list } }, methods: { addTodo(ev){ if(this.things !== ""){ var item = { title:this.things, isChecked:false, } this.list.push(item) } this.things = ""; }, deleteTodo(item){ var index = this.list.indexOf(item); this.list.splice(index,1); }, editTodo(item){ this.beforeTitle = item.title; this.editItem = item }, edited(item){ this.editItem = "" }, cancel(item){ item.title = this.beforeTitle; this.editItem = ""; this.beforeTitle = "" } }, directives:{ "focus":{ update(el,binding){ if(binding.value){ el.focus() } } } } });
這是一個(gè)基本的Vue實(shí)例,el是和DOM元素連接的掛載點(diǎn),data是代理數(shù)據(jù),在DOM的內(nèi)容中如果要用到代理數(shù)據(jù)就用{{xxx}}表示,比如{{list}},{{visibility}},而當(dāng)data中的代理數(shù)據(jù)出現(xiàn)在DOM標(biāo)簽里的時(shí)候就不需要用花括號(hào)。
new Vue({ el: ".main", data: { list:list, things:"", editItem:"", beforeTitle:"", visibility:"all", inputId:"", } })
Vue要用大的方法都放在methods部分
methods: { addTodo(ev){ if(this.things !== ""){ var item = { title:this.things, isChecked:false, } this.list.push(item) } this.things = ""; }, deleteTodo(item){ var index = this.list.indexOf(item); this.list.splice(index,1); }, editTodo(item){ this.beforeTitle = item.title; this.editItem = item }, edited(item){ this.editItem = "" }, cancel(item){ item.title = this.beforeTitle; this.editItem = ""; this.beforeTitle = "" } }
還有計(jì)算屬性
computed:{ unCheckedLength(){ return this.list.filter(function(item){ return item.isChecked == false }).length }, filteredList(){ return filter[this.visibility] ? filter[this.visibility](this.list) : list } }
觀察屬性
watch:{ list:{ handler:function(){ store.save("todolist",this.list) }, deep:true } }
自定義屬性
directives:{ "focus":{ update(el,binding){ if(binding.value){ el.focus() } } } }
在HTML中要綁定這些數(shù)據(jù),Vue也提供了一套指令:
v-bind綁定一個(gè)或多個(gè)特性,一般用于綁定class和style, v-on 綁定事件, v-show,v-if都是根據(jù)條件渲染元素,v-for是渲染列表…等等,我就不一一贅述了。可以去Vue中文官網(wǎng)看,講的很詳細(xì)。
以上是“Vue如何制作Todo List網(wǎng)頁(yè)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:Vue如何制作TodoList網(wǎng)頁(yè)
網(wǎng)頁(yè)路徑:http://jinyejixie.com/article38/ppeosp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、小程序開(kāi)發(fā)、關(guān)鍵詞優(yōu)化、App設(shè)計(jì)、靜態(tài)網(wǎng)站、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)