本文主要是介绍自定义指令Custom Directives,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<script setup lang='ts'>
import { ref } from "vue"const state = ref(false)/*** Implement the custom directive* Make sure the input element focuses/blurs when the 'state' is toggled*
*/
// 以v开头的驼峰式命名的变量都可以作为一个自定义指令
const VFocus = {mounted:(el)=>el.focus(), // el是指令绑定到的元素updated:(el,binding)=>{ // 包含传递给指令的值、指令修饰符modifiers(v-directive.upper)、指令参数arg(v-directive:foo)等属性binding.value ? el.focus() : el.blur()}
}setInterval(() => {state.value = !state.value
}, 2000)</script><template><input v-focus="state" type="text">
</template>
自定义指令的对象提供了7个钩子函数:
created、beforeMounted、 mounted、 beforeUpdate、updated、beforeUnmount、unmounted
大多数情况仅需要使用mounted和updated。
对比vue的8个生命周期:
beforeCreate、created、beforeMount、mounted、beforeUpdate、updated、beforeUnmount、unmounted
这篇关于自定义指令Custom Directives的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!