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/api/index.md
+16-4
Original file line number
Diff line number
Diff line change
@@ -347,6 +347,8 @@ type: api
347
347
})
348
348
```
349
349
350
+
<pclass="tip">Note that __you should not use an arrow function with the `data` property__ (e.g. `data: () => { return { a: this.myProp }}`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.myProp` will be undefined.</p>
351
+
350
352
-**See also:**[Reactivity in Depth](/guide/reactivity.html)
351
353
352
354
### props
@@ -418,6 +420,8 @@ type: api
418
420
419
421
Computed properties to be mixed into the Vue instance. All getters and setters have their `this` context automatically bound to the Vue instance.
420
422
423
+
<pclass="tip">Note that __you should not use an arrow function to define a computed property__ (e.g. `aDouble: () => this.a * 2`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.a` will be undefined.</p>
424
+
421
425
Computed properties are cached, and only re-computed on reactive dependency changes.
422
426
423
427
-**Example:**
@@ -458,6 +462,8 @@ type: api
458
462
459
463
Methods to be mixed into the Vue instance. You can access these methods directly on the VM instance, or use them in directive expressions. All methods will have their `this` context automatically bound to the Vue instance.
460
464
465
+
<pclass="tip">Note that __you should not use an arrow function to define a method__ (e.g. `plus: () => this.a++`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.a` will be undefined.</p>
466
+
461
467
-**Example:**
462
468
463
469
```js
@@ -488,16 +494,18 @@ type: api
488
494
```js
489
495
var vm =newVue({
490
496
data: {
491
-
a:1
497
+
a:1,
498
+
b:2,
499
+
c:3
492
500
},
493
501
watch: {
494
-
'a':function (val, oldVal) {
502
+
a:function (val, oldVal) {
495
503
console.log('new: %s, old: %s', val, oldVal)
496
504
},
497
505
// string method name
498
-
'b':'someMethod',
506
+
b:'someMethod',
499
507
// deep watcher
500
-
'c': {
508
+
c: {
501
509
handler:function (val, oldVal) { /* ... */ },
502
510
deep:true
503
511
}
@@ -506,6 +514,8 @@ type: api
506
514
vm.a=2// -> new: 2, old: 1
507
515
```
508
516
517
+
<pclass="tip">Note that __you should not use an arrow function to define a watcher__ (e.g. `searchQuery: newValue => this.updateAutocomplete(newValue)`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.updateAutocomplete` will be undefined.</p>
All lifecycle hooks automatically have their `this` context bound to the instance, so that you can access data, computed properties, and methods. This means __you should not use an arrow function to define a lifecycle method__ (e.g. `created: () => this.fetchTodos()`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.fetchTodos` will be undefined.
Copy file name to clipboardExpand all lines: src/guide/instance.md
+2
Original file line number
Diff line number
Diff line change
@@ -73,6 +73,8 @@ vm.$watch('a', function (newVal, oldVal) {
73
73
})
74
74
```
75
75
76
+
<pclass="tip">Note that __you should not use arrow functions on an instance property or callback__ (e.g. `vm.$watch('a', newVal => this.myMethod())`). The reason is arrow functions bind the parent context, so `this` will not be the Vue instance as you expect and `this.myMethod` will be undefined.</p>
77
+
76
78
Consult the [API reference](/api) for the full list of instance properties and methods.
0 commit comments