Vue通過(guò)封裝全局獲取焦點(diǎn)指令
在main.js封裝v-focus指令
// 封裝全局指令 focus
Vue.directive('focus', {
// 指令所在的dom元素,被插入到頁(yè)面中時(shí)觸發(fā)
inserted (el) {
el.focus()
}
})使用
<template>
<div class="my-tag">
<input
v-if="isEdit"
v-focus
ref="inp"
class="input"
type="text"
placeholder="輸入標(biāo)簽"
:value="value"
@blur="isEdit = false"
@keyup.enter="handleEnter"
/>
<div
v-else
@dblclick="handleClick"
class="text">
{{ value }}
</div>
</div>
</template>
<script>
export default {
props: {
value: String
},
data () {
return {
isEdit: false
}
},
methods: {
handleClick () {
// 雙擊后,切換到顯示狀態(tài) (Vue是異步dom更新)
this.isEdit = true
// // 等dom更新完了,再獲取焦點(diǎn)
// this.$nextTick(() => {
// // 立刻獲取焦點(diǎn)
// this.$refs.inp.focus()
// })
},
handleEnter (e) {
// 非空處理
if (e.target.value.trim() === '') return alert('標(biāo)簽內(nèi)容不能為空')
// 子傳父,將回車時(shí),[輸入框的內(nèi)容] 提交給父組件更新
// 由于父組件是v-model,觸發(fā)事件,需要觸發(fā) input 事件
this.$emit('input', e.target.value)
// 提交完成,關(guān)閉輸入狀態(tài)
this.isEdit = false
}
}
}
</script>
<style lang="less" scoped>
.my-tag {
cursor: pointer;
.input {
appearance: none;
outline: none;
border: 1px solid #ccc;
width: 100px;
height: 40px;
box-sizing: border-box;
padding: 10px;
color: #666;
&::placeholder {
color: #666;
}
}
}
</style><template>
<table class="my-table">
<thead>
<tr>
<slot name="head"></slot>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in data" :key="item.id">
<slot name="body" :item="item" :index="index" ></slot>
</tr>
</tbody>
</table>
</template>
<script>
export default {
props: {
data: {
type: Array,
required: true
}
}
};
</script>
<style lang="less" scoped>
.my-table {
width: 100%;
border-spacing: 0;
img {
width: 100px;
height: 100px;
object-fit: contain;
vertical-align: middle;
}
th {
background: #f5f5f5;
border-bottom: 2px solid #069;
}
td {
border-bottom: 1px dashed #ccc;
}
td,
th {
text-align: center;
padding: 10px;
transition: all .5s;
&.red {
color: red;
}
}
.none {
height: 100px;
line-height: 100px;
color: #999;
}
}
</style><template>
<div class="table-case">
<MyTable :data="goods">
<template #head>
<th>編號(hào)</th>
<th>名稱</th>
<th>圖片</th>
<th width="100px">標(biāo)簽</th>
</template>
<template #body="{ item, index }">
<td>{{ index + 1 }}</td>
<td>{{ item.name }}</td>
<td>
<img
:src="item.picture"
/>
</td>
<td>
<MyTag v-model="item.tag"></MyTag>
</td>
</template>
</MyTable>
</div>
</template>
<script>
// my-tag 標(biāo)簽組件的封裝
// 1. 創(chuàng)建組件 - 初始化
// 2. 實(shí)現(xiàn)功能
// (1) 雙擊顯示,并且自動(dòng)聚焦
// v-if v-else @dbclick 操作 isEdit
// 自動(dòng)聚焦:
// 1. $nextTick => $refs 獲取到dom,進(jìn)行focus獲取焦點(diǎn)
// 2. 封裝v-focus指令
// (2) 失去焦點(diǎn),隱藏輸入框
// @blur 操作 isEdit 即可
// (3) 回顯標(biāo)簽信息
// 回顯的標(biāo)簽信息是父組件傳遞過(guò)來(lái)的
// v-model實(shí)現(xiàn)功能 (簡(jiǎn)化代碼) v-model => :value 和 @input
// 組件內(nèi)部通過(guò)props接收, :value設(shè)置給輸入框
// (4) 內(nèi)容修改了,回車 => 修改標(biāo)簽信息
// @keyup.enter, 觸發(fā)事件 $emit('input', e.target.value)
// ---------------------------------------------------------------------
// my-table 表格組件的封裝
// 1. 數(shù)據(jù)不能寫死,動(dòng)態(tài)傳遞表格渲染的數(shù)據(jù) props
// 2. 結(jié)構(gòu)不能寫死 - 多處結(jié)構(gòu)自定義 【具名插槽】
// (1) 表頭支持自定義
// (2) 主體支持自定義
import MyTag from './components/MyTag.vue'
import MyTable from './components/MyTable.vue'
export default {
name: 'TableCase',
components: {
MyTag,
MyTable
},
data () {
return {
// 測(cè)試組件功能的臨時(shí)數(shù)據(jù)
tempText: '水杯',
tempText2: '鋼筆',
goods: [
{ id: 101, picture: 'https://yanxuan-item.nosdn.127.net/f8c37ffa41ab1eb84bff499e1f6acfc7.jpg', name: '梨皮朱泥三絕清代小品壺經(jīng)典款紫砂壺', tag: '茶具' },
{ id: 102, picture: 'https://yanxuan-item.nosdn.127.net/221317c85274a188174352474b859d7b.jpg', name: '全防水HABU旋鈕牛皮戶外徒步鞋山寧泰抗菌', tag: '男鞋' },
{ id: 103, picture: 'https://yanxuan-item.nosdn.127.net/cd4b840751ef4f7505c85004f0bebcb5.png', name: '毛茸茸小熊出沒,兒童羊羔絨背心73-90cm', tag: '兒童服飾' },
{ id: 104, picture: 'https://yanxuan-item.nosdn.127.net/56eb25a38d7a630e76a608a9360eec6b.jpg', name: '基礎(chǔ)百搭,兒童套頭針織毛衣1-9歲', tag: '兒童服飾' },
]
}
}
}
</script>
<style lang="less" scoped>
.table-case {
width: 1000px;
margin: 50px auto;
img {
width: 100px;
height: 100px;
object-fit: contain;
vertical-align: middle;
}
}
</style>以上就是Vue通過(guò)封裝全局獲取焦點(diǎn)指令的詳細(xì)內(nèi)容,更多關(guān)于Vue獲取焦點(diǎn)指令的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue SSR 即時(shí)編譯技術(shù)的實(shí)現(xiàn)
這篇文章主要介紹了Vue SSR 即時(shí)編譯技術(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
vue element實(shí)現(xiàn)多個(gè)Formt表單同時(shí)驗(yàn)證
這篇文章主要介紹了vue element實(shí)現(xiàn)多個(gè)Formt表單同時(shí)驗(yàn)證方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Vue實(shí)現(xiàn)數(shù)值型輸入框并限制長(zhǎng)度
這篇文章主要介紹了Vue實(shí)現(xiàn)數(shù)值型輸入框并限制長(zhǎng)度,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
vue el-table字段點(diǎn)擊出現(xiàn)el-input輸入框,失焦保存方式
這篇文章主要介紹了vue el-table字段點(diǎn)擊出現(xiàn)el-input輸入框,失焦保存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Vue3之組件狀態(tài)保持KeepAlive的簡(jiǎn)單使用
這篇文章主要介紹了Vue3之組件狀態(tài)保持KeepAlive的簡(jiǎn)單使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
vue3一個(gè)元素如何綁定兩個(gè)或多個(gè)事件
這篇文章主要介紹了vue3一個(gè)元素如何綁定兩個(gè)或多個(gè)事件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
electron-vite新一代electron開發(fā)構(gòu)建工具
這篇文章主要介紹了electron-vite新一代electron開發(fā)構(gòu)建工具,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Vue數(shù)組響應(yīng)式操作及高階函數(shù)使用代碼詳解
這篇文章主要介紹了Vue數(shù)組響應(yīng)式操作及高階函數(shù)使用代碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
vue.js實(shí)現(xiàn)的全選與全不選功能示例【基于elementui】
這篇文章主要介紹了vue.js實(shí)現(xiàn)的全選與全不選功能,結(jié)合實(shí)例形式分析了vue.js基于elementui實(shí)現(xiàn)全選與全不選功能的相關(guān)頁(yè)面渲染、初始化數(shù)據(jù)及功能函數(shù)等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12

