Skip to content

Commit 34ef0ce

Browse files
authored
update readme for caveats of class properties (vuejs#69)
* update readme for caveats of class properties * add description vue will bind methods * Fix typo
1 parent 6e017d2 commit 34ef0ce

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,39 @@ new Vue({
159159
})
160160
```
161161

162+
### Caveats of Class Properties
163+
164+
vue-class-component collects class properties as Vue instance data by instantiating the original constructor under the hood. While we can define instance data like native class manner, we sometimes need to know how it works.
165+
166+
For example, if you define an arrow function as a class property and access `this` in it, it will not work. This is because `this` is just a proxy object to Vue instance when initializing class properties:
167+
168+
```js
169+
@Component
170+
class MyComp extends Vue {
171+
foo = 123
172+
173+
bar = () => {
174+
// Does not update the expected property.
175+
// `this` value is not a Vue instance in fact.
176+
this.foo = 456
177+
}
178+
}
179+
```
180+
181+
You can simply define a method instead of a class property in that case because Vue will bind the instance automatically:
182+
183+
```js
184+
@Component
185+
class MyComp extends Vue {
186+
foo = 123
187+
188+
bar () {
189+
// Correctly update the expected property.
190+
this.foo = 456
191+
}
192+
}
193+
```
194+
162195
### Build the Example
163196

164197
``` bash

0 commit comments

Comments
 (0)