Vuejs 實(shí)現(xiàn)簡(jiǎn)易 todoList 功能 與 組件實(shí)例代碼
todoList
結(jié)合之前 Vuejs 基礎(chǔ)與語(yǔ)法
•使用 v-model 雙向綁定 input 輸入內(nèi)容與數(shù)據(jù) data
•使用 @click 和 methods 關(guān)聯(lián)事件
•使用 v-for 進(jìn)行數(shù)據(jù)循環(huán)展示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>TodoList</title> <script src="./vue.js"></script> </head> <body> <div id="root"> <div> <input v-model="inputValue"/> <button @click="handleSubmit">提交</button> </div> <ul> <li v-for="(item,index) of list" :key="index"> {{item}} </li> </ul> </div> <script> new Vue({ el: "#root", data: { inputValue: '', list: [] }, methods: { handleSubmit: function(){ this.list.push(this.inputValue) this.inputValue = '' } } }) </script> </body> </html>
todoList 組件拆分
Vuejs 組件相關(guān) 詳細(xì)參考組件基礎(chǔ)
全局組件
注冊(cè)全局組件,并在 HTML 中通過(guò)模板調(diào)用組件
//注冊(cè)全局組件 Vue.component('todo-item',{ template: '<li>item</li>' }) <ul> <!-- <li v-for="(item,index) of list" :key="index"> {{item}} </li> --> <todo-item></todo-item> <!-- 通過(guò)模板使用組件 --> </ul>
局部組件
在注冊(cè)了局部組件之后,直接通過(guò)模板調(diào)用是不可以的,必須要在最外層的 Vue 的實(shí)例中添加 components: { }進(jìn)行組件聲明。
//注冊(cè)局部組件 var TodoItem = { template: '<li>item</li>' } new Vue({ el: "#root", components: { //局部組件需要聲明的 components 'todo-item': TodoItem }, data: { inputValue: '', list: [] }, methods: { handleSubmit: function(){ this.list.push(this.inputValue) this.inputValue = '' } } })
即通過(guò)局部注冊(cè)的組件,需要在其他的 Vue 實(shí)例中使用該局部組件。必須使用 components 對(duì)該局部組件進(jìn)行注冊(cè)。
上面的實(shí)例中,要在 Vue 實(shí)例中使用 TodoItem 這個(gè)局部組件,就通過(guò) todo-item 這個(gè)標(biāo)簽來(lái)使用。當(dāng)在實(shí)例中 注冊(cè)好了以后,才可以在模板里面使用這個(gè)標(biāo)簽。這樣就算正確的使用了局部組件。
外部傳遞參數(shù)
給 todo-item 標(biāo)簽添加 :content 屬性,值為循環(huán)的每一項(xiàng)的內(nèi)容 "item",
這樣就可以吧 content 傳遞給 todo-item 這個(gè)組件
<todo-item v-for="(item,index) of list" :key="index" :content="item"></todo-item>
但是直接將組件改成是不行的
Vue.component('todo-item',{ template: '<li>{{content}}</li>' })
需要讓組件接收屬性,所以要在todo-item組件里面定義props屬性,其值為一個(gè)數(shù)組 'content' 。
其含義是,該組件接收從外部傳遞的進(jìn)來(lái)的名字叫做 content 的屬性
Vue.component('todo-item',{ props: ['content'], template: '<li>{{content}}</li>' })
組件與實(shí)例的關(guān)系
Vue 之中,每一個(gè)組件其實(shí)也是一個(gè) Vue 的實(shí)例。因此在 Vue 項(xiàng)目中,是一個(gè)個(gè)實(shí)例構(gòu)建而成的。
因此組件之中,也可以綁定 @click 事件,添加 methods 屬性。
Vue.component('todo-item',{ props: ['content'], template: '<li @click="handleClick">{{content}}</li>', methods: { handleClick: function(){ alert('clicked') } } })
同樣的實(shí)例也可以被稱作一個(gè)組件,那么我們這個(gè)根實(shí)例當(dāng)中的 template 模板是什么呢 ?
如果一個(gè) Vue 實(shí)例沒有模板,會(huì)到掛載點(diǎn)去找。如下實(shí)例,根實(shí)例會(huì)找到 #root 下面掛載點(diǎn)的所有內(nèi)容作為模板。
new Vue({ el: "#root", data: { inputValue: '', list: [] }, methods: { handleSubmit: function(){ this.list.push(this.inputValue) this.inputValue = '' } } })
為 todoList 添加刪除功能
通過(guò) 發(fā)布 / 訂閱,當(dāng)子組件點(diǎn)擊時(shí),通知父組件把數(shù)據(jù)刪除掉。在子組件中,發(fā)布自定義一個(gè) 'delete' 事件。調(diào)用 this.$emit 方法,并傳遞 index 的值。
子組件向外部進(jìn)行發(fā)布
//子組件 Vue.component('todo-item',{ props: ['content','index'], template: '<li @click="handleClick">{{content}}</li>', methods: { handleClick: function(){ //發(fā)布 this.$emit('delete', this.index) } } })
父組件在模板里創(chuàng)建子組件的時(shí)候,監(jiān)聽子組件向外觸發(fā)的 delete 事件,如果監(jiān)聽到 delete 事件,執(zhí)行 handleDelete 函數(shù)。
<todo-item v-for="(item,index) of list" :key="index" :content="item" :index="index" @delete="handleDelete"> <!-- 監(jiān)聽delete事件 --> </todo-item> <!-- 通過(guò)模板使用組件 -->
然后在父組件的 methods 中,寫好 handleDelete 方法。
//最外層實(shí)例,父組件 new Vue({ el: "#root", data: { inputValue: '', list: [] }, methods: { handleSubmit: function(){ this.list.push(this.inputValue) this.inputValue = '' }, handleDelete: function(index){ this.list.splice(index,1) //使用splice方法刪除list } } })
總結(jié)
以上所述是小編給大家介紹的Vuejs 實(shí)現(xiàn)簡(jiǎn)易 todoList 功能 與 組件,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解如何在vue項(xiàng)目中使用layui框架及采坑
這篇文章主要介紹了vue使用layui框架,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05詳解Vue3.0中ElementPlus<input輸入框自動(dòng)獲取焦點(diǎn)>
這篇文章主要給大家介紹了關(guān)于Vue3.0中ElementPlus<input輸入框自動(dòng)獲取焦點(diǎn)>的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue3.0具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-04-04vue 微信分享回調(diào)iOS和安卓回調(diào)出現(xiàn)錯(cuò)誤的解決
這篇文章主要介紹了vue 微信分享回調(diào)iOS和安卓回調(diào)出現(xiàn)錯(cuò)誤的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09基于vue2.0+vuex+localStorage開發(fā)的本地記事本示例
這篇文章主要介紹了基于vue2.0+vuex+localStorage開發(fā)的本地記事本示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02