--- title: Avoid Using Custom Directives on Components impact: HIGH impactDescription: Custom directives on multi-root components are silently ignored, causing unexpected behavior type: gotcha tags: [vue3, directives, components, multi-root, best-practices] --- # Avoid Using Custom Directives on Components **Impact: HIGH** - Using custom directives on components is not recommended and can lead to unexpected behavior. When applied to a multi-root component, the directive will be ignored and a warning will be thrown. Unlike attributes, directives cannot be passed to a different element with `v-bind="$attrs"`. Custom directives are designed for direct DOM manipulation on native HTML elements, not Vue components. ## Task Checklist - [ ] Only apply custom directives to native HTML elements, not components - [ ] If a component needs directive-like behavior, consider making it part of the component's API - [ ] For components, use props and events instead of directives - [ ] If you must use a directive on a component, ensure it has a single root element **Incorrect:** ```vue ``` **Correct:** ```vue ``` ## When a Directive on Component Works Directives only work reliably on components with a **single root element**. The directive applies to the root node, similar to fallthrough attributes: ```vue ``` However, this is still not recommended because: 1. It's fragile - refactoring to multi-root breaks the directive silently 2. It's unclear which element receives the directive 3. The component author may not expect external DOM manipulation ## Better Patterns ### Option 1: Component Prop ```vue ``` ### Option 2: Exposed Method ```vue ``` ## Reference - [Vue.js Custom Directives - Usage on Components](https://vuejs.org/guide/reusability/custom-directives#usage-on-components)