You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/guide/reusability/custom-directives.md
+77-23
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,24 @@
1
1
# Custom Directives {#custom-directives}
2
2
3
3
<scriptsetup>
4
-
constvFocus= {
4
+
constvHighlight= {
5
5
mounted:el=> {
6
-
el.focus()
6
+
el.classList.add('is-highlight')
7
7
}
8
8
}
9
9
</script>
10
10
11
+
<style>
12
+
.vt-docp.is-highlight {
13
+
margin-bottom: 0;
14
+
}
15
+
16
+
.is-highlight {
17
+
background-color: yellow;
18
+
color: black;
19
+
}
20
+
</style>
21
+
11
22
## Introduction {#introduction}
12
23
13
24
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
20
31
21
32
```vue
22
33
<script setup>
23
-
// enables v-focus in templates
24
-
const vFocus = {
25
-
mounted: (el) => el.focus()
34
+
// enables v-highlight in templates
35
+
const vHighlight = {
36
+
mounted: (el) => {
37
+
el.classList.add('is-highlight')
38
+
}
26
39
}
27
40
</script>
28
41
29
42
<template>
30
-
<input v-focus />
43
+
<p v-highlight>This sentence is important!</p>
31
44
</template>
32
45
```
33
46
@@ -36,44 +49,42 @@ const vFocus = {
36
49
<divclass="options-api">
37
50
38
51
```js
39
-
constfocus= {
40
-
mounted: (el) =>el.focus()
52
+
consthighlight= {
53
+
mounted: (el) =>el.classList.add('is-highlight')
41
54
}
42
55
43
56
exportdefault {
44
57
directives: {
45
-
// enables v-focus in template
46
-
focus
58
+
// enables v-highlight in template
59
+
highlight
47
60
}
48
61
}
49
62
```
50
63
51
64
```vue-html
52
-
<input v-focus />
65
+
<p v-highlight>This sentence is important!</p>
53
66
```
54
67
55
68
</div>
56
69
57
70
<divclass="demo">
58
-
<inputv-focusplaceholder="This should be focused" />
71
+
<pv-highlight>This sentence is important!</p>
59
72
</div>
60
73
61
-
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.
62
-
63
74
<divclass="composition-api">
64
75
65
-
In `<script setup>`, any camelCase variable that starts with the `v` prefix can be used as a custom directive. In the example above, `vFocus` can be used in the template as `v-focus`.
76
+
In `<script setup>`, any camelCase variable that starts with the `v` prefix can be used as a custom directive. In the example above, `vHighlight` can be used in the template as `v-highlight`.
66
77
67
-
If not using `<script setup>`, custom directives can be registered using the `directives` option:
78
+
If you are not using `<script setup>`, custom directives can be registered using the `directives` option:
68
79
69
80
```js
70
81
exportdefault {
71
82
setup() {
72
83
/*...*/
73
84
},
74
85
directives: {
75
-
// enables v-focus in template
76
-
focus: {
86
+
// enables v-highlight in template
87
+
highlight: {
77
88
/* ... */
78
89
}
79
90
}
@@ -94,14 +105,58 @@ It is also common to globally register custom directives at the app level:
94
105
constapp=createApp({})
95
106
96
107
// make v-focus usable in all components
97
-
app.directive('focus', {
108
+
app.directive('highlight', {
98
109
/* ... */
99
110
})
100
111
```
101
112
102
-
:::tip
103
-
Custom directives should only be used when the desired functionality can only be achieved via direct DOM manipulation. Prefer declarative templating using built-in directives such as `v-bind` when possible because they are more efficient and server-rendering friendly.
104
-
:::
113
+
## When to use custom directives {#when-to-use}
114
+
115
+
Custom directives should only be used when the desired functionality can only be achieved via direct DOM manipulation.
116
+
117
+
A common example of this is a `v-focus` custom directive that brings an element into focus.
118
+
119
+
<divclass="composition-api">
120
+
121
+
```vue
122
+
<script setup>
123
+
// enables v-focus in templates
124
+
const vFocus = {
125
+
mounted: (el) => el.focus()
126
+
}
127
+
</script>
128
+
129
+
<template>
130
+
<input v-focus />
131
+
</template>
132
+
```
133
+
134
+
</div>
135
+
136
+
<divclass="options-api">
137
+
138
+
```js
139
+
constfocus= {
140
+
mounted: (el) =>el.focus()
141
+
}
142
+
143
+
exportdefault {
144
+
directives: {
145
+
// enables v-focus in template
146
+
focus
147
+
}
148
+
}
149
+
```
150
+
151
+
```vue-html
152
+
<input v-focus />
153
+
```
154
+
155
+
</div>
156
+
157
+
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!
158
+
159
+
Declarative templating with built-in directives such as `v-bind` is recommended when possible because they are more efficient and server-rendering friendly.
0 commit comments