diff --git a/src/guide/reusability/custom-directives.md b/src/guide/reusability/custom-directives.md index 206a840a5c..742968f274 100644 --- a/src/guide/reusability/custom-directives.md +++ b/src/guide/reusability/custom-directives.md @@ -1,13 +1,24 @@ # Custom Directives {#custom-directives} + + ## Introduction {#introduction} In addition to the default set of directives shipped in core (like `v-model` or `v-show`), Vue also allows you to register your own custom directives. @@ -20,14 +31,16 @@ A custom directive is defined as an object containing lifecycle hooks similar to ```vue ``` @@ -36,35 +49,33 @@ const vFocus = {
```js -const focus = { - mounted: (el) => el.focus() +const highlight = { + mounted: (el) => el.classList.add('is-highlight') } export default { directives: { - // enables v-focus in template - focus + // enables v-highlight in template + highlight } } ``` ```vue-html - +

This sentence is important!

```
- +

This sentence is important!

-Assuming you haven't clicked elsewhere on the page, the input above should be auto-focused. This directive is more useful than the `autofocus` attribute because it works not just on page load - it also works when the element is dynamically inserted by Vue. -
-In ` + + +``` + +
+ +
+ +```js +const focus = { + mounted: (el) => el.focus() +} + +export default { + directives: { + // enables v-focus in template + focus + } +} +``` + +```vue-html + +``` + +
+ +This directive is more useful than the `autofocus` attribute because it works not just on page load - it also works when the element is dynamically inserted by Vue! + +Declarative templating with built-in directives such as `v-bind` is recommended when possible because they are more efficient and server-rendering friendly. ## Directive Hooks {#directive-hooks} @@ -214,7 +269,6 @@ app.directive('demo', (el, binding) => { Using custom directives on components is not recommended. Unexpected behaviour may occur when a component has multiple root nodes. ::: - When used on components, custom directives will always apply to a component's root node, similar to [Fallthrough Attributes](/guide/components/attrs). ```vue-html