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
`vm.$events.$off` | `event, callback` | _Alias for `off`_
24
26
`vm.$events.remove` | `event, callback` | _Alias for `off`_
25
27
28
+
### Component Options
29
+
30
+
```js
31
+
/// Some component.vue
32
+
exportdefault {
33
+
// registers event handlers on 'created'
34
+
on: {
35
+
handler(arg) {
36
+
console.log(args)
37
+
}
38
+
},
39
+
// Same as previous, but is called only once.
40
+
once: {
41
+
handler() {
42
+
console.log('Just once')
43
+
}
44
+
}
45
+
}
46
+
```
47
+
26
48
## Examples
27
49
28
50
```js
@@ -54,31 +76,6 @@ beforeDestroy() {
54
76
}
55
77
```
56
78
57
-
P.S.: Alternative way to set your event handlers is through the `on` and `once` Vue constructor options. In that way you shouldn't worry about removing event handlers on `beforeDestroy`.
58
-
59
-
```js
60
-
/// Component 1
61
-
62
-
methods: {
63
-
eventHandler(payload) {
64
-
console.log('Yay, events work!', payload);
65
-
},
66
-
eventHandlerOnce(payload) {
67
-
console.log('This will be called just once!');
68
-
}
69
-
},
70
-
on: {
71
-
test() {
72
-
this.eventHandler();
73
-
}
74
-
},
75
-
once: {
76
-
test() {
77
-
this.eventHandlerOnce();
78
-
}
79
-
}
80
-
```
81
-
82
79
```js
83
80
/// Component 2
84
81
@@ -96,5 +93,21 @@ created() {
96
93
}
97
94
```
98
95
96
+
Alternative way to set your event handlers is through the `on` and `once` Vue constructor options. In that way you may not worry about removing event handlers in `beforeDestroy` hook. This approach makes code cleaner and reduces the amount of boilerplate code.
0 commit comments