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
+17-5Lines changed: 17 additions & 5 deletions
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.
573
+
562
574
### beforeCreate
563
575
564
576
-**Type:**`Function`
@@ -829,7 +841,7 @@ type: api
829
841
830
842
-**Details:**
831
843
832
-
The data object that the Vue instance is observing. You can swap it with a new object. The Vue instance proxies access to the properties on its data object.
844
+
The data object that the Vue instance is observing. The Vue instance proxies access to the properties on its data object.
Copy file name to clipboardExpand all lines: src/guide/comparison.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ With that said, we hope you can feel confident in the fairness of the review bel
26
26
27
27
### Performance Profiles
28
28
29
-
In every real-world scenario that we've tested so far, Vue outperforms React by a fair margin (usually at least 20-50% faster, though in some cases much more than that). We could now link to benchmarks - but frankly, all benchmarks are flawed in some way and very few resemble what you'd write in a real application. Instead, let's break it down.
29
+
In every real-world scenario that we've tested so far, Vue outperforms React by a fair margin. If your eyebrows are raising right now, read further. We'll breakdown why (and even include a benchmark developed in collaboration with the React team).
Copy file name to clipboardExpand all lines: src/guide/index.md
+11-7Lines changed: 11 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ order: 2
6
6
7
7
## What is Vue.js?
8
8
9
-
Vue (pronounced /vjuː/, like **view**) is a **progressive framework** for building user interfaces. Unlike other monolithic frameworks, Vue is designed from the ground up to be incrementally adoptable. The core library is focused on the view layer only, and is very easy to pick up and integrate with other libraries or existing projects. On the other hand, Vue is also perfectly capable of powering sophisticated Single-Page Applications when used in combination with [modern tooling](application.html) and [supporting libraries](https://github.com/vuejs/awesome-vue#libraries--plugins).
9
+
Vue (pronounced /vjuː/, like **view**) is a **progressive framework** for building user interfaces. Unlike other monolithic frameworks, Vue is designed from the ground up to be incrementally adoptable. The core library is focused on the view layer only, and is very easy to pick up and integrate with other libraries or existing projects. On the other hand, Vue is also perfectly capable of powering sophisticated Single-Page Applications when used in combination with [modern tooling](single-file-components.html) and [supporting libraries](https://github.com/vuejs/awesome-vue#libraries--plugins).
10
10
11
11
If you are an experienced frontend developer and want to know how Vue compares to other libraries/frameworks, check out the [Comparison with Other Frameworks](comparison.html).
12
12
@@ -51,34 +51,38 @@ In addition to text interpolation, we can also bind element attributes like this
51
51
52
52
```html
53
53
<divid="app-2">
54
-
<spanv-bind:id="id">Inspect me</span>
54
+
<spanv-bind:title="message">
55
+
Hover your mouse over me for a few seconds to see my dynamically bound title!
56
+
</span>
55
57
</div>
56
58
```
57
59
```js
58
60
var app2 =newVue({
59
61
el:'#app-2',
60
62
data: {
61
-
id:'inspect-me'
63
+
message:'You loaded this page on '+newDate()
62
64
}
63
65
})
64
66
```
65
67
{% raw %}
66
68
<divid="app-2"class="demo">
67
-
<spanv-bind:id="id">Inspect me</span>
69
+
<spanv-bind:title="message">
70
+
Hover your mouse over me for a few seconds to see my dynamically bound title!
71
+
</span>
68
72
</div>
69
73
<script>
70
74
var app2 =newVue({
71
75
el:'#app-2',
72
76
data: {
73
-
id:'inspect-me'
77
+
message:'You loaded this page on '+newDate()
74
78
}
75
79
})
76
80
</script>
77
81
{% endraw %}
78
82
79
-
Here we are encountering something new. The `v-bind` attribute you are seeing is called a **directive**. Directives are prefixed with `v-` to indicate that they are special attributes provided by Vue, and as you may have guessed, they apply special reactive behavior to the rendered DOM. Here it is basically saying "bind this element's `id` attribute to the `id` property on the Vue instance."
83
+
Here we are encountering something new. The `v-bind` attribute you are seeing is called a **directive**. Directives are prefixed with `v-` to indicate that they are special attributes provided by Vue, and as you may have guessed, they apply special reactive behavior to the rendered DOM. Here it is basically saying "keep this element's `title` attribute up-to-date with the `message` property on the Vue instance."
80
84
81
-
Use the browser devtools to inspect the element above - you should see that it has the id `inspect-me`. And yes, it would update if you modify `app2.id` in the console.
85
+
If you open up your JavaScript console again and enter `app2.message = 'some new message'`, you'll once again see that the bound HTML - in this case the `title` attribute - has been updated.
Copy file name to clipboardExpand all lines: src/guide/installation.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ Also available on [jsdelivr](//cdn.jsdelivr.net/vue/{{vue_version}}/vue.js) or [
36
36
37
37
## NPM
38
38
39
-
NPM is the recommended installation method when building large scale applications with Vue. It pairs nicely with module bundlers such as [Webpack](http://webpack.github.io/) or [Browserify](http://browserify.org/). Vue also provides accompanying tools for authoring [Single File Components](application.html#Single-File-Components).
39
+
NPM is the recommended installation method when building large scale applications with Vue. It pairs nicely with module bundlers such as [Webpack](http://webpack.github.io/) or [Browserify](http://browserify.org/). Vue also provides accompanying tools for authoring [Single File Components](single-file-components.html).
Copy file name to clipboardExpand all lines: src/guide/instance.md
+2Lines changed: 2 additions & 0 deletions
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.
Copy file name to clipboardExpand all lines: src/guide/migration.md
+40-13Lines changed: 40 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -332,7 +332,7 @@ Most use cases of mutating a prop can be replaced by one of these options:
332
332
333
333
### Props on a Root Instance <sup>deprecated</sup>
334
334
335
-
On root Vue instances (i.e. instances created with `new Vue({ ... })`), you must use `propsData` instead instead of `props`.
335
+
On root Vue instances (i.e. instances created with `new Vue({ ... })`), you must use `propsData` instead of `props`.
336
336
337
337
{% raw %}
338
338
<divclass="upgrade-path">
@@ -653,7 +653,7 @@ Directives have a greatly reduced scope of responsibility: they are now only use
653
653
Some of the most notable differences include:
654
654
655
655
- Directives no longer have instances. This means there's no more `this` inside directive hooks. Instead, they receive everything they might need as arguments. If you really must persist state across hooks, you can do so on `el`.
656
-
- Options such as `acceptStatement`, `deep`, `priority`, etc are all deprecated.
656
+
- Options such as `acceptStatement`, `deep`, `priority`, etc are all deprecated. To replace `twoWay` directives, see [this example](#Two-Way-Filters-deprecated).
657
657
- Some of the current hooks have different behavior and there are also a couple new hooks.
658
658
659
659
Fortunately, since the new directives are much simpler, you can master them more easily. Read the new [Custom Directives guide](custom-directive.html) to learn more.
@@ -665,6 +665,29 @@ Fortunately, since the new directives are much simpler, you can master them more
The `.literal` modifier has been removed, as the same can be easily achieved by just providing a string literal as the value.
671
+
672
+
For example, you can update:
673
+
674
+
```js
675
+
<p v-my-directive.literal="foo bar baz"></p>
676
+
```
677
+
678
+
to just:
679
+
680
+
```html
681
+
<pv-my-directive="'foo bar baz'"></p>
682
+
```
683
+
684
+
{% raw %}
685
+
<divclass="upgrade-path">
686
+
<h4>Upgrade Path</h4>
687
+
<p>Run the <ahref="https://github.com/vuejs/vue-migration-helper">migration helper</a> on your codebase to find examples of the `.literal` modifier on a directive.</p>
688
+
</div>
689
+
{% endraw %}
690
+
668
691
## Transitions
669
692
670
693
### `transition` Attribute <sup>deprecated</sup>
@@ -702,6 +725,10 @@ If you need to stagger list transitions, you can control timing by setting and a
702
725
703
726
## Events
704
727
728
+
### `events` option <sup>deprecated</sup>
729
+
730
+
The `events` option has been deprecated. Event handlers should now be registered in the `created` hook instead. Check out the [`$dispatch` and `$broadcast` migration guide](#dispatch-and-broadcast-deprecated) for a detailed example.
The new, more concise way to configure `keyCodes` is through`Vue.config.keyCodes`. For example:
@@ -1016,32 +1043,32 @@ In many cases though, you'll still run into strange behavior (e.g. `0.035.toFixe
1016
1043
1017
1044
### Two-Way Filters <sup>deprecated</sup>
1018
1045
1019
-
Some users have enjoyed using two-way filters with `v-model` to create interesting inputs with very little code. While seemingly simple however, two-way filters can also hide a great deal of complexity - and even encourage poor UX by delaying state updates. Instead, components are recommended as a more explicit and feature-rich way of creating custom inputs.
1046
+
Some users have enjoyed using two-way filters with `v-model` to create interesting inputs with very little code. While _seemingly_ simple however, two-way filters can also hide a great deal of complexity - and even encourage poor UX by delaying state updates. Instead, components wrapping an input are recommended as a more explicit and feature-rich way of creating custom inputs.
1020
1047
1021
-
We'll now walk through an example of a filter provided by the community and offer tips for how to migrate away from two-way filters. Here's a simple currency filter:
1048
+
As an example, we'll now walk the migration of a two-way currency filter:
It mostly works well, but the delayed state updates can cause strange behavior. For example, try entering `99.999` into one of those inputs. When the input loses focus, its value will update to `$100.00`. When looking at the calculated total however, you'll see that `99.999` is what's stored in our data. The version of reality that the user sees is out of sync!
1052
+
It mostly works well, but the delayed state updates can cause strange behavior. For example, click on the `Result` tab and try entering `9.999` into one of those inputs. When the input loses focus, its value will update to `$10.00`. When looking at the calculated total however, you'll see that `9.999` is what's stored in our data. The version of reality that the user sees is out of sync!
1026
1053
1027
-
To start transitioning towards a more robust solution using Vue 2.0, let's first refactor this filter into a component that uses it:
1054
+
To start transitioning towards a more robust solution using Vue 2.0, let's first wrap this filter in a new `<currency-input>` component:
This allows us add behavior that wasn't possible with a filter alone, such as selecting the content of an input on focus. Now the next step will be to extract the business logic from the filter:
1058
+
This allows us add behavior that a filter alone couldn't encapsulate, such as selecting the content of an input on focus. Now the next step will be to extract the business logic from the filter. Below, we pull everything out into an external [`currencyValidator` object](https://gist.github.com/chrisvfritz/5f0a639590d6e648933416f90ba7ae4e):
This increased modularity not only makes it easier to migrate to Vue 2, but also allows currency parsing and formatting to be:
1036
1063
1037
1064
- unit tested in isolation from your Vue code
1038
1065
- used by other parts of your application, such as to validate the payload to an API endpoint
1039
1066
1040
-
Having this validator extracted out, we can also more comfortably build it up into a more robust solution. We've now eliminated the state quirks and have even made it impossible for users to enter anything wrong, similar to what the browser's native number input tries to do.
1067
+
Having this validator extracted out, we've also more comfortably built it up into a more robust solution. The state quirks have been eliminated and it's actually impossible for users to enter anything wrong, similar to what the browser's native number input tries to do.
1041
1068
1042
1069
We're still limited however, by filters and by Vue 1.0 in general, so let's complete the upgrade to Vue 2.0:
Copy file name to clipboardExpand all lines: src/guide/unit-testing.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ order: 22
6
6
7
7
## Setup and Tooling
8
8
9
-
Anything compatible with a module-based build system will work, but if you're looking for a specific recommendation, try the [Karma](http://karma-runner.github.io/0.12/index.html) test runner. It has a lot of community plugins, including support for [Webpack](https://github.com/webpack/karma-webpack) and [Browserify](https://github.com/Nikku/karma-browserify). For detailed setup, please refer to each project's respective documentation, though these example Karma configurations for [Webpack](https://github.com/vuejs/vue-loader-example/blob/master/build/karma.conf.js) and [Browserify](https://github.com/vuejs/vueify-example/blob/master/karma.conf.js) may help you get started.
9
+
Anything compatible with a module-based build system will work, but if you're looking for a specific recommendation, try the [Karma](http://karma-runner.github.io) test runner. It has a lot of community plugins, including support for [Webpack](https://github.com/webpack/karma-webpack) and [Browserify](https://github.com/Nikku/karma-browserify). For detailed setup, please refer to each project's respective documentation, though these example Karma configurations for [Webpack](https://github.com/vuejs/vue-loader-example/blob/master/build/karma.conf.js) and [Browserify](https://github.com/vuejs/vueify-example/blob/master/karma.conf.js) may help you get started.
0 commit comments