vue組件系列之TagsInput詳解
簡(jiǎn)介
TagsInput 是一種可編輯的輸入框,通過(guò)回車或者分號(hào)來(lái)分割每個(gè)標(biāo)簽,用回退鍵刪除上一個(gè)標(biāo)簽。用 vue 來(lái)實(shí)現(xiàn)還是比較簡(jiǎn)單的。
先看效果圖,下面會(huì)一步一步實(shí)現(xiàn)他。

注:以下代碼需要vue-cli環(huán)境才能執(zhí)行
(一)偽造一個(gè)輸入框
因?yàn)閱涡械奈谋究蛑荒苷故炯兾谋荆詧D里面的標(biāo)簽實(shí)際上都是 html元素,用vue模板來(lái)寫(xiě)的話,是這樣的:
<template>
<div class="muli-tags" @click='focus'>
<button class='btn' v-for='(tag, index) in tags' :key='index'>
{{tag}}
</button>
<input type="text" ref='input' v-model='current'>
</div>
</template>
<script>
export default {
name: 'TagsInput',
methods: {
focus () {
this.$refs.input.focus()
},
},
data () {
return {
tags: [],
current: ''
}
}
}
</script>
<style lang='less'>
.muli-tags{
padding: 5px 10px;
display: block;
border: 1px solid #ccc;
input{
background: transparent;
}
}
.btn{
margin: 0 5px 3px 0;
padding: 4px 5px;
background: #fff;
border: 1px solid #eee;
box-shadow: 0 0 4px;
}
</style>
(二)監(jiān)聽(tīng)輸入
在偽造好一個(gè)輸入框之后,我們對(duì)輸入框的事件進(jìn)行處理,
- 回車和逗號(hào)會(huì)把input的值添加到tags數(shù)組,然后清空input
- 添加值之前,判斷tags數(shù)組是否已經(jīng)包含同名的值
- 按回退鍵,刪除最近的一個(gè)標(biāo)簽
// @keydown.188 188代表是是分號(hào)鍵的keyCode
<input type="text"
ref='input'
@keyup.enter="add"
@keydown.delete="del"
@keydown.188='split'
v-model='current'>
methods: {
// 按下分號(hào)鍵的時(shí)候,需要阻止默認(rèn)事件,否則會(huì)出現(xiàn)分號(hào)
split (e) {
e.preventDefault()
this.add(e)
},
add (e) {
const val = e.target.value
if (!val) return
// 如果已經(jīng)存在相同tag,不再添加
if (this.tags.indexOf(val) > -1) return
// 把輸入值添加到tag,并清空文本框
this.tags.push(val)
this.current = ''
},
del (e) {
// 當(dāng)文本框內(nèi)沒(méi)有值,再按回退鍵,則刪除最后一個(gè)tag
if (!e.target.value.length) {
this.tags.pop()
}
},
}
(三)刪除標(biāo)簽
前面都是通過(guò)鍵盤來(lái)操作標(biāo)簽,鼠標(biāo)點(diǎn)擊標(biāo)簽應(yīng)該也是可以刪除的
<button class='btn' v-for='(tag, index) in tags' :key='index' @click='delTag(index)'>{{tag}} <span>x</span></button>
methods: {
// 刪除點(diǎn)擊的標(biāo)簽
delTag (index) {
this.tags.splice(index, 1)
}
}
(四)自定義 v-model
通過(guò)上面的步驟,一個(gè) tagsinput 組件就已經(jīng)做好了,再給他添加自定義的 v-model ,讓他可以像input一樣響應(yīng)表單數(shù)據(jù)。
// props
props: {
value: Array,
required: true,
default: () => []
}
// computed
computed: {
tags () {
return this.value.slice()
}
}
// methods
methods: {
// 刪除點(diǎn)擊的標(biāo)簽
delTag (index) {
this.tags.splice(index, 1)
this.$emit('input', this.tags)
}
}
(五)完整代碼
// TagsInput.vue
<template>
<div class="muli-tags" @click='focus'>
<button class='btn' v-for='(tag, index) in tags' :key='index' @click='delTag(index)'>{{tag}} <span>x</span></button>
<input type="text"
ref='input'
@keyup.enter="add"
@keydown.delete="del"
@keydown.188='split'
v-model='current'>
</div>
</template>
<script>
export default {
props: {
value: Array,
required: true,
default: () => []
},
methods: {
focus () {
this.$refs.input.focus()
},
split (e) {
e.preventDefault()
this.add(e)
},
add (e) {
const val = e.target.value
if (!val) return
if (this.tags.indexOf(val) > -1) return
this.tags.push(val)
this.$emit('input', this.tags)
this.current = ''
},
del (e) {
if (!e.target.value.length) {
this.tags.pop()
this.$emit('input', this.tags)
}
},
delTag (index) {
this.tags.splice(index, 1)
this.$emit('input', this.tags)
}
},
computed: {
tags () {
return this.value.slice()
}
},
data () {
return {
current: ''
}
}
}
</script>
<style lang='less'>
.muli-tags{
padding: 5px 10px;
display: block;
border: 1px solid #ccc;
input{
background: transparent;
}
.btn{
margin: 0 5px 3px 0;
padding: 4px 5px;
background: #fff;
border: 1px solid #eee;
box-shadow: 0 0 4px;
}
}
</style>
作為組件被調(diào)用,這樣就可以看到像文章開(kāi)頭那幅圖一樣的組件了。
// 父組件
<template>
<tags-input v-model='tags'/>
</template>
<script>
import TagsInput from './TagsInput.vue'
export default {
components: {
TagsInput
},
data () {
return {
tags: ['tag1', 'tag2', 'tag3']
}
}
}
</script>
總結(jié)
到此這篇關(guān)于vue組件TagsInput的文章就介紹到這了,更多相關(guān)vue組件TagsInput內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue2.0利用vue-resource上傳文件到七牛的實(shí)例代碼
本篇文章主要介紹了Vue2.0利用vue-resource上傳文件到七牛的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
vue3使用vuex實(shí)現(xiàn)頁(yè)面實(shí)時(shí)更新數(shù)據(jù)實(shí)例教程(setup)
在前端開(kāi)發(fā)中往往會(huì)遇到頁(yè)面需要實(shí)時(shí)刷新數(shù)據(jù)的情況,給用戶最新的數(shù)據(jù)展示,這篇文章主要給大家介紹了關(guān)于vue3使用vuex實(shí)現(xiàn)頁(yè)面實(shí)時(shí)更新數(shù)據(jù)(setup)的相關(guān)資料,需要的朋友可以參考下2022-09-09
Vue?el-menu?左側(cè)菜單導(dǎo)航功能的實(shí)現(xiàn)
這篇文章主要介紹了Vue?el-menu?左側(cè)菜單導(dǎo)航功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Vue.js 無(wú)限滾動(dòng)列表性能優(yōu)化方案
這篇文章主要介紹了Vue.js 無(wú)限滾動(dòng)列表性能優(yōu)化方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
vue中使用loadsh的debounce防抖函數(shù)問(wèn)題
這篇文章主要介紹了vue中使用loadsh的debounce防抖函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
解決vue項(xiàng)目F5刷新mounted里的函數(shù)不執(zhí)行問(wèn)題
今天小編就為大家分享一篇解決vue項(xiàng)目F5刷新mounted里的函數(shù)不執(zhí)行問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
在Vue中使用Viser說(shuō)明(基于AntV-G2可視化引擎)
這篇文章主要介紹了在Vue中使用Viser說(shuō)明(基于AntV-G2可視化引擎),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10

