Skip to content

Commit b055026

Browse files
authored
add warnings about using arrow functions on instance properties (vuejs#448)
1 parent 7a5e303 commit b055026

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/api/index.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ type: api
347347
})
348348
```
349349

350+
<p class="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+
350352
- **See also:** [Reactivity in Depth](/guide/reactivity.html)
351353

352354
### props
@@ -418,6 +420,8 @@ type: api
418420

419421
Computed properties to be mixed into the Vue instance. All getters and setters have their `this` context automatically bound to the Vue instance.
420422

423+
<p class="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+
421425
Computed properties are cached, and only re-computed on reactive dependency changes.
422426

423427
- **Example:**
@@ -458,6 +462,8 @@ type: api
458462

459463
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.
460464

465+
<p class="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+
461467
- **Example:**
462468

463469
```js
@@ -488,16 +494,18 @@ type: api
488494
``` js
489495
var vm = new Vue({
490496
data: {
491-
a: 1
497+
a: 1,
498+
b: 2,
499+
c: 3
492500
},
493501
watch: {
494-
'a': function (val, oldVal) {
502+
a: function (val, oldVal) {
495503
console.log('new: %s, old: %s', val, oldVal)
496504
},
497505
// string method name
498-
'b': 'someMethod',
506+
b: 'someMethod',
499507
// deep watcher
500-
'c': {
508+
c: {
501509
handler: function (val, oldVal) { /* ... */ },
502510
deep: true
503511
}
@@ -506,6 +514,8 @@ type: api
506514
vm.a = 2 // -> new: 2, old: 1
507515
```
508516

517+
<p class="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>
518+
509519
- **See also:** [Instance Methods - vm.$watch](#vm-watch)
510520

511521
## Options / DOM
@@ -559,6 +569,8 @@ type: api
559569

560570
## Options / Lifecycle Hooks
561571

572+
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.
573+
562574
### beforeCreate
563575

564576
- **Type:** `Function`

src/guide/instance.md

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ vm.$watch('a', function (newVal, oldVal) {
7373
})
7474
```
7575

76+
<p class="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+
7678
Consult the [API reference](/api) for the full list of instance properties and methods.
7779

7880
## Instance Lifecycle Hooks

0 commit comments

Comments
 (0)