vue3自定義指令看完這篇就入門了
前言
這篇文章介紹vue組件中的自定義指令!看完不會你打我。哈哈哈,開玩笑的??!
1. 什么是自定義指令
vue 官方提供了 v-for、v-model、v-if 等常用的內(nèi)置指令。除此之外 vue 還允許開發(fā)者自定義指令。
vue 中的自定義指令分為兩類,分別是:
- 私有自定義指令
- 全局自定義指令
2. 聲明私有自定義指令的語法
在每個 vue 組件中,可以在 directives 節(jié)點下聲明私有自定義指令。示例代碼如下:
<script> export default { directives: { // 自定義私有指令focus,在使用的時候要用 v-focus 。 focus: { mounted(el) { el.focus() }, }, }, } </script>
3. 使用自定義指令
在使用自定義指令時,需要加上 v- 前綴。示例代碼如下:
<!-- 聲明自定義私有指令focus,在使用的時候要用 v-focus 。 --> <input v-focus/>
4. 聲明全局自定義指令的語法
全局共享的自定義指令需要通過“單頁面應(yīng)用程序的實例對象”進(jìn)行聲明,示例代碼如下:
import { createApp } from 'vue' const app = createApp({ /* ... */ }) // 注冊(對象形式的指令) app.directive('my-directive', { /* 自定義指令鉤子 */ }) // 注冊(函數(shù)形式的指令) app.directive('my-directive', () => { /* ... */ }) // 得到一個已注冊的指令 const myDirective = app.directive('my-directive')
5. updated 函數(shù)
mounted 函數(shù)只在元素第一次插入 DOM 時被調(diào)用,當(dāng) DOM 更新時 mounted 函數(shù)不會被觸發(fā)。 updated 函數(shù)會在每次 DOM 更新完成后被調(diào)用。示例代碼如下:
// 聲明全局自定義指令 app.directive('focus', { mounted(el) { console.log('mounted') el.focus() }, updated(el) { console.log('updated') el.focus() }, })
注意:在 vue2 的項目中使用自定義指令時,【 mounted 要換成 bind 】【 updated 要換成 update 】
6. 函數(shù)簡寫
如果 mounted 和updated 函數(shù)中的邏輯完全相同,則可以簡寫成如下格式:
app.directive('focus', (el) => { // 在 mounted 和 updated 都會觸發(fā)這個函數(shù)方法 el.focus() })
7. 指令的參數(shù)值
在綁定指令時,可以通過“等號”的形式為指令綁定具體的參數(shù)值,示例代碼如下:
// 自定義 v-color 指令 app.directive('color', (el, binding) => { // binding.value 就是通過 = 綁定的值,在傳值的時候傳到這 binding.value el.style.color = binding.value })
附:下面根據(jù)自定義指令知識點衍生的一個小例子
該例子沒有特別的技術(shù)難點。只是為了驗證一下這兩天學(xué)習(xí)的vue3部分知識點,存粹是一個練手記錄~~~
//示例
<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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue監(jiān)聽滾動實現(xiàn)錨點定位(雙向)示例
今天小編大家分享一篇Vue監(jiān)聽滾動實現(xiàn)錨點定位(雙向)示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11VUE中v-on:click事件中獲取當(dāng)前dom元素的代碼
這篇文章主要介紹了VUE中v-on:click事件中獲取當(dāng)前dom元素的代碼,文中同時給大家提到了v-on:click獲取當(dāng)前事件對象元素的方法,需要的朋友可以參考下2018-08-08vue.js中v-on:textInput無法執(zhí)行事件問題的解決過程
大家都知道vue.js通過v-on完成事件處理與綁定,但最近使用v-on的時候遇到了一個問題,所以下面這篇文章主要給大家介紹了關(guān)于vue.js中v-on:textInput無法執(zhí)行事件問題的解決過程,需要的朋友可以參考下。2017-07-07vue安裝和使用scss及sass與scss的區(qū)別詳解
這篇文章主要介紹了vue安裝和使用教程,用了很久css預(yù)編譯器,但是一直不太清楚到底用的sass還是scss,直到有天被問住了有點尷尬,感興趣的朋友一起看看吧2018-10-10一文讀懂vue動態(tài)屬性數(shù)據(jù)綁定(v-bind指令)
這篇文章主要介紹了vue動態(tài)屬性數(shù)據(jù)綁定(v-bind指令)的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07