Custom directives 让你将 low-level DOM manipulation 封装为可复用的 v-* 属性。当你需要直接访问元素时使用它们(focus、scroll、third-party DOM libraries)— 这些是 components/props 无法自然覆盖的功能。
定义本地指令
vue
<script setup>
// a directive is an object of lifecycle hooks; in <script setup>, name it vXxx
const vFocus = {
mounted(el) {
el.focus(); // `el` is the raw DOM element
},
};
</script>
<template>
<input v-focus /> <!-- autofocuses on mount -->
</template>
命名约定:变量 变成指令 。
