vue3自定義指令看完這篇就入門(mén)了
前言
這篇文章介紹vue組件中的自定義指令!看完不會(huì)你打我。哈哈哈,開(kāi)玩笑的?。?/p>
1. 什么是自定義指令
vue 官方提供了 v-for、v-model、v-if 等常用的內(nèi)置指令。除此之外 vue 還允許開(kāi)發(fā)者自定義指令。
vue 中的自定義指令分為兩類(lèi),分別是:
- 私有自定義指令
- 全局自定義指令
2. 聲明私有自定義指令的語(yǔ)法
在每個(gè) vue 組件中,可以在 directives 節(jié)點(diǎn)下聲明私有自定義指令。示例代碼如下:
<script> export default { directives: { // 自定義私有指令focus,在使用的時(shí)候要用 v-focus 。 focus: { mounted(el) { el.focus() }, }, }, } </script>
3. 使用自定義指令
在使用自定義指令時(shí),需要加上 v- 前綴。示例代碼如下:
<!-- 聲明自定義私有指令focus,在使用的時(shí)候要用 v-focus 。 --> <input v-focus/>
4. 聲明全局自定義指令的語(yǔ)法
全局共享的自定義指令需要通過(guò)“單頁(yè)面應(yīng)用程序的實(shí)例對(duì)象”進(jìn)行聲明,示例代碼如下:
import { createApp } from 'vue' const app = createApp({ /* ... */ }) // 注冊(cè)(對(duì)象形式的指令) app.directive('my-directive', { /* 自定義指令鉤子 */ }) // 注冊(cè)(函數(shù)形式的指令) app.directive('my-directive', () => { /* ... */ }) // 得到一個(gè)已注冊(cè)的指令 const myDirective = app.directive('my-directive')
5. updated 函數(shù)
mounted 函數(shù)只在元素第一次插入 DOM 時(shí)被調(diào)用,當(dāng) DOM 更新時(shí) mounted 函數(shù)不會(huì)被觸發(fā)。 updated 函數(shù)會(huì)在每次 DOM 更新完成后被調(diào)用。示例代碼如下:
// 聲明全局自定義指令 app.directive('focus', { mounted(el) { console.log('mounted') el.focus() }, updated(el) { console.log('updated') el.focus() }, })
注意:在 vue2 的項(xiàng)目中使用自定義指令時(shí),【 mounted 要換成 bind 】【 updated 要換成 update 】
6. 函數(shù)簡(jiǎn)寫(xiě)
如果 mounted 和updated 函數(shù)中的邏輯完全相同,則可以簡(jiǎn)寫(xiě)成如下格式:
app.directive('focus', (el) => { // 在 mounted 和 updated 都會(huì)觸發(fā)這個(gè)函數(shù)方法 el.focus() })
7. 指令的參數(shù)值
在綁定指令時(shí),可以通過(guò)“等號(hào)”的形式為指令綁定具體的參數(shù)值,示例代碼如下:
// 自定義 v-color 指令 app.directive('color', (el, binding) => { // binding.value 就是通過(guò) = 綁定的值,在傳值的時(shí)候傳到這 binding.value el.style.color = binding.value })
附:下面根據(jù)自定義指令知識(shí)點(diǎn)衍生的一個(gè)小例子
該例子沒(méi)有特別的技術(shù)難點(diǎn)。只是為了驗(yàn)證一下這兩天學(xué)習(xí)的vue3部分知識(shí)點(diǎn),存粹是一個(gè)練手記錄~~~
//示例
<template> <p> 改變方向:<input type="text" v-model="direction" /> </p> <p> 改變數(shù)值:<input type="range" min="0" :max="maxNum" v-model="pinPadding" /> </p> <p> <button @click="reset">重置</button> </p> <div style="position: relative;border: 1px solid red;width: 800px;height: 400px;margin: 0 auto;"> <p v-pin:[direction]="pinPadding">數(shù)據(jù):{{pinPadding}},方向:{{direction}}</p> </div> </template> <script> import two from './components/two.vue' import { ref, reactive, defineComponent, computed } from 'vue' export default ({ name: 'lycApp', components: { two }, setup(prop, context) { const direction = ref('left') const pinPadding = ref(0) const reset = () => { direction.value = 'left' pinPadding.value = 0 } const maxNum = computed(()=>{ if(direction.value == 'right' || direction.value == 'left'){ return 650 }else{ return 360 } }) return { direction, pinPadding, reset, maxNum } }, directives: { pin: { mounted(el, binding) { el.style.position = 'absolute' const s = binding.arg el.style[s] = binding.value + 'px' }, updated(el, binding) { el.removeAttribute('style') el.style.position = 'absolute' const s = binding.arg el.style[s] = binding.value + 'px' } } } }) </script>
//效果圖
可以在紅色線框內(nèi)隨便改變方向跟距離。
總結(jié)
到此這篇關(guān)于vue3自定義指令的文章就介紹到這了,更多相關(guān)vue3自定義指令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
聊聊element-ui 側(cè)邊欄的router問(wèn)題
這篇文章主要介紹了關(guān)于element-ui 側(cè)邊欄的router問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05Vue3中reactive丟失響應(yīng)式問(wèn)題詳解
在vue3中,如果你用reactive聲明了一個(gè)對(duì)象,用另一個(gè)對(duì)象直接給它賦值,那么它就會(huì)失去響應(yīng)式,下面這篇文章主要給大家介紹了關(guān)于Vue3中reactive丟失響應(yīng)式問(wèn)題的相關(guān)資料,需要的朋友可以參考下2023-03-03Vue.js實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Vue.js實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Vue監(jiān)聽(tīng)滾動(dòng)實(shí)現(xiàn)錨點(diǎn)定位(雙向)示例
今天小編大家分享一篇Vue監(jiān)聽(tīng)滾動(dòng)實(shí)現(xiàn)錨點(diǎn)定位(雙向)示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11VUE中v-on:click事件中獲取當(dāng)前dom元素的代碼
這篇文章主要介紹了VUE中v-on:click事件中獲取當(dāng)前dom元素的代碼,文中同時(shí)給大家提到了v-on:click獲取當(dāng)前事件對(duì)象元素的方法,需要的朋友可以參考下2018-08-08vue用BMap百度地圖實(shí)現(xiàn)即時(shí)搜索功能
這篇文章主要為大家詳細(xì)介紹了vue用BMap百度地圖實(shí)現(xiàn)即時(shí)搜索功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09vue.js中v-on:textInput無(wú)法執(zhí)行事件問(wèn)題的解決過(guò)程
大家都知道vue.js通過(guò)v-on完成事件處理與綁定,但最近使用v-on的時(shí)候遇到了一個(gè)問(wèn)題,所以下面這篇文章主要給大家介紹了關(guān)于vue.js中v-on:textInput無(wú)法執(zhí)行事件問(wèn)題的解決過(guò)程,需要的朋友可以參考下。2017-07-07vue安裝和使用scss及sass與scss的區(qū)別詳解
這篇文章主要介紹了vue安裝和使用教程,用了很久css預(yù)編譯器,但是一直不太清楚到底用的sass還是scss,直到有天被問(wèn)住了有點(diǎn)尷尬,感興趣的朋友一起看看吧2018-10-10一文讀懂vue動(dòng)態(tài)屬性數(shù)據(jù)綁定(v-bind指令)
這篇文章主要介紹了vue動(dòng)態(tài)屬性數(shù)據(jù)綁定(v-bind指令)的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07