vue3 自定義指令詳情
一、注冊自定義指令
以下實例都是實現(xiàn)一個輸入框自動獲取焦點的自定義指令。
1.1、全局自定義指令
在vue2中,全局自定義指令通過 directive
掛載到 Vue 對象上,使用 Vue.directive('name',opt)
。
實例1:Vue2 全局自定義指令
Vue.directive('focus',{ inserted:(el)=>{ el.focus() } })
inserted
是鉤子函數(shù),在綁定元素插入父節(jié)點時執(zhí)行。
在 vue3
中,vue 實例通過createApp
創(chuàng)建,所以全局自定義指令的掛載方式也改變了, directive
被掛載到 app上。
實例2:Vue3 全局自定義指令
//全局自定義指令 app.directive('focus',{ mounted(el){ el.focus() } }) //組件使用 <input type="text" v-focus />
1.2、局部自定義指令
在組件內(nèi)部,使用 directives
引入的叫做局部自定義指令。Vue2
和 Vue3
的自定義指令引入是一模一樣的。
實例3:局部自定義指令
<script> //局部自定義指令 const defineDir = { focus:{ mounted(el){ el.focus() } } } export default { directives:defineDir, setup(){} } </script>
二、自定義指令中的生命周期鉤子函數(shù)
一個指令定義對象可以提供如下幾個鉤子函數(shù)(都是可選的,根據(jù)需要引入)
created
:綁定元素屬性或事件監(jiān)聽器被應(yīng)用之前調(diào)用。該指令需要附加需要在普通的 v-on 事件監(jiān)聽器前調(diào)用的事件監(jiān)聽器時,這很有用。beforeMounted
:當(dāng)指令第一次綁定到元素并且在掛載父組件之前執(zhí)行。mounted
:綁定元素的父組件被掛載之后調(diào)用。beforeUpdate
:在更新包含組件的 VNode 之前調(diào)用。updated
:在包含組件的 VNode 及其子組件的 VNode 更新后調(diào)用。beforeUnmounted
:在卸載綁定元素的父組件之前調(diào)用unmounted
:當(dāng)指令與元素解除綁定且父組件已卸載時,只調(diào)用一次。
實例3:測試指令內(nèi)生命周期函數(shù)執(zhí)行
<template> <div> <input type="text" v-focus v-if="show"><br> <button @click="changStatus">{{show?'隱藏':'顯示'}}</button> </div> </template> //局部自定義指令 const autoFocus = { focus:{ created(){ console.log('created'); }, beforeMount(){ console.log('beforeMount'); }, mounted(el){ console.log('mounted'); }, beforeUpdated(){ console.log('beforeUpdated') }, updated(){ console.log('updated'); }, beforeUnmount(){ console.log('beforeUnmount'); }, unmounted(){ console.log('unmounted'); } }, } import { ref } from 'vue' export default { directives:autoFocus, setup(){ const show = ref(true) return { show, changStatus(){ show.value = !show.value } } } }
通過點擊按鈕,我們發(fā)現(xiàn)創(chuàng)建 input
元素的時候,會觸發(fā) created
、beforeMount
和 mounted
三個鉤子函數(shù)。
隱藏 input
元素的時候,會觸發(fā) beforeUnmount
和 unmounted
。
然而我們添加的 beforeUpdate
和 updated
函數(shù)并沒有執(zhí)行。
此時我們把 input
元素上的 v-if
修改成 v-show
就會執(zhí)行上述兩個方法了,具體的執(zhí)行情況自行驗證下。
從 vue2 升級到 vue3 ,自定義指令的生命周期鉤子函數(shù)發(fā)生了改變,具體變化如下:
bind
函數(shù)被替換成了beforeMounted
。update
被移除。componentUpdated
被替換成了updated
。unbind
被替換成了unmounted
。inserted
被移除。
三、自定義指令鉤子函數(shù)的參數(shù)
鉤子函數(shù)被賦予了以下參數(shù):
el
:指令所綁定的元素,可以直接操作DOM
。binding
:是一個對象,包含該指令的所有信息。
binding 包含的屬性具體的分別為:
arg
自定義指令的參數(shù)名。value
自定義指令綁定的值。oldValue
指令綁定的前一個值。dir
被執(zhí)行的鉤子函數(shù)modifiers
:一個包含修飾符的對象。
<template> <div> <div v-fixed >定位</div> </div> </template> <script> //自定義指令動態(tài)參數(shù) const autoFocus = { fixed:{ beforeMount(el,binding){ console.log('el',el) console.log('binding',binding) } } } export default { directives:autoFocus, setup(){ } } </script>
四、自定義指令參數(shù)
自定義指令的也可以帶參數(shù),參數(shù)可以是動態(tài)的,參數(shù)可以根據(jù)組件實例數(shù)據(jù)進(jìn)行實時更新。
實例4:自定義指令動態(tài)參數(shù)
<template> <div> <div v-fixed:pos="posData" style="width:100px;height:100px;background:grey">定位</div> </div> </template> <script> //自定義指令動態(tài)參數(shù) const autoFocus = { fixed:{ beforeMount(el,binding){ el.style.position = "fixed" el.style.left = binding.value.left+'px' el.style.top = binding.value.top + 'px' } } } export default { directives:autoFocus, setup(){ const posData = { left:20, top:200 } return { posData, } } } </script>
什么時候需要自定義指令?
- 需要對普通 DOM 元素進(jìn)行底層操作,這時候就會用到自定義指令。
- 需要將某些功能在指定DOM元素上使用,但對于需要操作大量DOM元素或者大變動時候,推薦使用組件,而不是指令。
到此這篇關(guān)于 vue3 自定義指令詳情的文章就介紹到這了,更多相關(guān) vue3 自定義指令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實現(xiàn)裁切圖片同時實現(xiàn)放大、縮小、旋轉(zhuǎn)功能
這篇文章主要介紹了vue實現(xiàn)裁切圖片同時實現(xiàn)放大、縮小、旋轉(zhuǎn)功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03vue使用keep-alive進(jìn)行組件緩存方法詳解(組件不緩存問題解決)
keep-alive包裹動態(tài)組件時,會緩存不活動的組件實例,而不是銷毀它們,下面這篇文章主要給大家介紹了關(guān)于vue使用keep-alive進(jìn)行組件緩存方法(組件不緩存問題解決)的相關(guān)資料,需要的朋友可以參考下2022-09-09Vue+element使用row-class-name修改el-table某一行解決背景色無效的方法
本文主要介紹了Vue+element使用row-class-name修改el-table某一行解決背景色無效的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Vue2.0使用嵌套路由實現(xiàn)頁面內(nèi)容切換/公用一級菜單控制頁面內(nèi)容切換(推薦)
這篇文章主要介紹了Vue2.0使用嵌套路由實現(xiàn)頁面內(nèi)容切換/公用一級菜單控制頁面內(nèi)容切換,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05