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/custom-directive.md
+26-24Lines changed: 26 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -6,21 +6,43 @@ order: 16
6
6
7
7
## Intro
8
8
9
-
In addition to the default set of directives shipped in core (`v-model` and `v-show`), Vue also allows you to register your own custom directives. Directives provide a reusable way of binding behavior that requires low-level DOM access, such as focusing on an input element.
9
+
In addition to the default set of directives shipped in core (`v-model` and `v-show`), Vue also allows you to register your own custom directives. Directives provide a reusable way of binding behavior that requires low-level DOM access, such as focusing on an input element, like this one:
10
10
11
-
Here's a simple example:
11
+
{% raw %}
12
+
<divid="simplest-directive-example"class="demo">
13
+
<inputv-focus>
14
+
</div>
15
+
<script>
16
+
Vue.directive('focus', {
17
+
bind:function (el) {
18
+
Vue.nextTick(function () {
19
+
el.focus()
20
+
})
21
+
}
22
+
})
23
+
newVue({
24
+
el:'#simplest-directive-example'
25
+
})
26
+
</script>
27
+
{% endraw %}
28
+
29
+
When the page loads, that element gains focus. In fact, if you haven't clicked on anything else since visiting this page, the input above should be focused now. Now let's build the directive that accomplishes this:
12
30
13
31
```js
32
+
// Register a global custom directive called v-focus
14
33
Vue.directive('focus', {
34
+
// When the directive is first bound to the element...
15
35
bind:function (el) {
36
+
// After the next render...
16
37
Vue.nextTick(function () {
38
+
// Focus the element
17
39
el.focus()
18
40
})
19
41
}
20
42
})
21
43
```
22
44
23
-
This registers a global custom directive `v-focus`. If you want to register a directive locally instead, components also accept a `directives` option:
45
+
If you want to register a directive locally instead, components also accept a `directives` option:
24
46
25
47
```js
26
48
directives: {
@@ -30,32 +52,12 @@ directives: {
30
52
}
31
53
```
32
54
33
-
Now in your template, you can use the `v-focus` attribute on any element, like this:
55
+
Then in a template, you can use the new`v-focus` attribute on any element, like this:
34
56
35
57
```html
36
58
<inputv-focus>
37
59
```
38
60
39
-
When the page loads, that element will gain focus. In fact, if you haven't clicked on anything else since visiting this page, the input below should be focused now:
40
-
41
-
{% raw %}
42
-
<divid="simplest-directive-example"class="demo">
43
-
<inputv-focus>
44
-
</div>
45
-
<script>
46
-
Vue.directive('focus', {
47
-
bind:function (el) {
48
-
Vue.nextTick(function () {
49
-
el.focus()
50
-
})
51
-
}
52
-
})
53
-
newVue({
54
-
el:'#simplest-directive-example'
55
-
})
56
-
</script>
57
-
{% endraw %}
58
-
59
61
## Hook Functions
60
62
61
63
A directive definition object can provide several hook functions (all optional):
0 commit comments