vue實(shí)現(xiàn)todolist功能、todolist組件拆分及todolist的刪除功能
•簡單todolist功能的實(shí)現(xiàn)
用戶點(diǎn)擊提交按鈕時(shí),將input框的內(nèi)容顯示在下方的list中,同時(shí)清空list中內(nèi)容。
<body>
<div id="root">
<div>
<input v-model="inputValue"/>
<button @click="submit">submit</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:{
submit:function(){
this.list.push(this.inputValue);
this.inputValue=''
}
}
})
</script>
</body>
“input”輸入框和“inputValue”數(shù)據(jù)雙向綁定
通過click事件,來講"inputValue"中的內(nèi)容添加到"list"中
向列表中添加數(shù)據(jù)用 push( ) this.list.pust(this.inputValue)
每次添加"list"后,把input內(nèi)容清空
•todolist組件拆分
1. Vue.component是全局組件,是vue提供的創(chuàng)建組件的方法。里面可以寫模板:template
2. 創(chuàng)建組件之后,可以直接使用。比如創(chuàng)建的組件名字是'todo-item',就可以使用<todo-item></todo-item>
3.
<div id="root">
<ul>
<todo-item></todo-item>
</ul>
</div>
<script>
Vue.component('todo-item',{
template:'<li>item<li>'
})
new Vue({
el:"root"
})
</script>
4.局部組件var TodoItem={}這里只寫了部分代碼
5.
div id="root">
<ul>
<todo-item></todo-item>
</ul>
</div>
<script>
var TodoItem={
template:'<li>item<li>'
}
new Vue({
el:"root",
components:{
'todo-item':TodoItem
}
})
</script>
6.
如果想在其他vue里面使用這個(gè)局部組件,需要在vue里對該局部組件進(jìn)行注冊
7.當(dāng)用組件來實(shí)現(xiàn)最上面的那個(gè)todolist功能時(shí),需要進(jìn)行參數(shù)的傳遞和接收,用content和props
8.
<body>
<div id="root">
<div>
<input v-model="inputValue"/>
<button @click="submit">submit</button>
</div>
<ul>
<todo-item v-for="(item,index) of list"
:key="index"
:content="item"
>
</todo-item>
</ul>
</div>
<script>
Vue.component('todo-item',{
props:['content'],
template:'<li>{{content}}<li>'
})
new Vue({
el:"#root",
data:{
inputValue:'',
list:[]
},
methods:{
submit:function(){
this.list.push(this.inputValue)
this.inputValue=' '
}
}
})
</script>
</body>
9.
這里面用content來傳遞item的值,用props來接收content的值。實(shí)現(xiàn)數(shù)據(jù)的傳遞功能
• todolist的刪除功能
1.
繼續(xù)上面的代碼,當(dāng)點(diǎn)擊list數(shù)據(jù)的時(shí)候,實(shí)現(xiàn)list的刪除功能
2.
首先來捋一下邏輯:創(chuàng)建的最外層的大組件/實(shí)例中使用了一個(gè)小的組件todoitem,我們可以認(rèn)為最外層的大組件為父組件,里面的小組件為子組件。
3.
我們在父組件中通過屬性的形式給子組件傳遞了具體的內(nèi)容,然后子組件進(jìn)行接收父組件傳遞的內(nèi)容,然后在子組件的模板中進(jìn)行顯示。
4.
要想實(shí)現(xiàn)子組件中數(shù)據(jù)的刪除,需要?jiǎng)h除父組件中對應(yīng)的數(shù)據(jù)。當(dāng)點(diǎn)擊子組件的數(shù)據(jù)時(shí),要實(shí)現(xiàn)子組件和父組件的通信,來在父組件中進(jìn)行刪除對應(yīng)數(shù)據(jù)的操作。
5.
<body>
<div id="root">
<div>
<input v-model="inputValue"/>
<button @click="submit">submit</button>
</div>
<ul>
<todo-item v-for="(item,index) of list"
:key="index"
:content="item"
:index="index"
@delete="handleDelete"
>
</todo-item>
</ul>
</div>
<script>
Vue.component('todo-item',{
props:['content','index'],
template:'<li @clicl="handleClick">{{content}}<li>',
methods:{
handleClick:function(){
this.$emit('delete',this.index)
}
}
})
new Vue({
el:"#root",
data:{
inputValue='',
list=[]
},
methods:{
submit:function(){
this.list.push(this.inputValue)
this.inputValue=' '
},
handleDelete:function(index){
this.list.splice(index,1)
}
}
})
</script>
</body>
總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)todolist功能、todolist組件拆分及todolist的刪除功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
element-ui中el-input只輸入數(shù)字(包括整數(shù)和小數(shù))
開發(fā)中有時(shí)候需要input只能輸入數(shù)字,下面這篇文章主要給大家介紹了關(guān)于element-ui中el-input只輸入數(shù)字(包括整數(shù)和小數(shù))的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
vue 純js監(jiān)聽滾動(dòng)條到底部的實(shí)例講解
今天小編就為大家分享一篇vue 純js監(jiān)聽滾動(dòng)條到底部的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue中進(jìn)行數(shù)據(jù)篩選與搜索功能實(shí)現(xiàn)常用的方法
表格常用功能經(jīng)常有字段篩選、更多字段篩選彈框來過濾出我們所需要的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Vue中進(jìn)行數(shù)據(jù)篩選與搜索功能實(shí)現(xiàn)常用的方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
vue3.0 vue.config.js 配置基礎(chǔ)的路徑問題
這篇文章主要介紹了vue3.0 vue.config.js 配置基礎(chǔ)的路徑問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

