Vue通過封裝全局獲取焦點(diǎn)指令
在main.js封裝v-focus指令
// 封裝全局指令 focus
Vue.directive('focus', {
// 指令所在的dom元素,被插入到頁面中時(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)簽信息是父組件傳遞過來的
// v-model實(shí)現(xiàn)功能 (簡化代碼) v-model => :value 和 @input
// 組件內(nèi)部通過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 {
// 測試組件功能的臨時(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通過封裝全局獲取焦點(diǎn)指令的詳細(xì)內(nèi)容,更多關(guān)于Vue獲取焦點(diǎn)指令的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue中ElementUI結(jié)合transform使用時(shí)如何修復(fù)el-select彈框定位不準(zhǔn)確問題
這篇文章主要介紹了Vue中ElementUI結(jié)合transform使用時(shí)如何修復(fù)el-select彈框定位不準(zhǔn)確問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
如何將iconfont圖標(biāo)引入到vant的Tabbar標(biāo)簽欄里邊
這篇文章主要介紹了如何將iconfont圖標(biāo)引入到vant的Tabbar標(biāo)簽欄里邊,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue + Vite項(xiàng)目通過/dist子路徑訪問首頁空白問題的完整分析與解決方案
在前端工程化實(shí)踐中,將Vue應(yīng)用構(gòu)建后部署到 Nginx是一件再常見不過的事情,然而,當(dāng)項(xiàng)目需要以子路徑形式部署時(shí),卻經(jīng)常會(huì)遇到頁面能訪問,但首頁內(nèi)容為空白,本文將圍繞通過域名/dist/訪問 Vue+Vite項(xiàng)目首頁空白這一典型問題,并給出一套完整、可復(fù)用的解決方案2026-01-01
vue集成高德地圖amap-jsapi-loader的實(shí)現(xiàn)
本文主要介紹了vue集成高德地圖amap-jsapi-loader的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Vue+ElementUi實(shí)現(xiàn)點(diǎn)擊表格鏈接頁面跳轉(zhuǎn)和路由效果
這篇文章主要介紹了Vue+ElementUi實(shí)現(xiàn)點(diǎn)擊表格中鏈接進(jìn)行頁面跳轉(zhuǎn)和路由,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03
Vue利用遞歸組件實(shí)現(xiàn)嵌套回復(fù)功能
這篇文章主要為大家詳細(xì)介紹了Vue如何利用遞歸組件實(shí)現(xiàn)嵌套回復(fù)功能,文章的示例代碼講解詳細(xì),如果你也在獨(dú)立做全棧項(xiàng)目,希望這些經(jīng)驗(yàn)?zāi)軒湍闵俨葞讉€(gè)坑2026-05-05
vue中Form 表單的 resetFields() 失效原因及問題解決
在Vue項(xiàng)目中,使用formRef.value.resetFields()方法重置表單時(shí)可能遇到不起作用的問題,下面就來介紹一下如何解決,感興趣的可以了解一下2024-09-09

