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
Reorganize sidebar and some content; cannibalize reactivity page (#1086)
* reorganize sidebar and some content; cannibalize reactivity page
* Fix typo in instance.md
* Fix typo in instance.md
* Change errorMessage: null to error: null in instance.md
* Fix typo in reactivity.md.hidden
* remove redundant note about filters
<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>
@@ -744,6 +744,17 @@ if (version === 2) {
744
744
745
745
Called after the instance has just been mounted where `el` is replaced by the newly created `vm.$el`. If the root instance is mounted to an in-document element, `vm.$el` will also be in-document when `mounted` is called.
746
746
747
+
Note that `mounted` does **not** guarantee that all child components have also been mounted. If you want to wait until the entire view has been rendered, you can use [vm.$nextTick](#vm-nextTick) inside of `mounted`:
748
+
749
+
``` js
750
+
mounted: function () {
751
+
this.$nextTick(function () {
752
+
// Code that will run only after the
753
+
// entire view has been rendered
754
+
})
755
+
}
756
+
```
757
+
747
758
**This hook is not called during server-side rendering.**
The component's DOM will have been updated when this hook is called, so you can perform DOM-dependent operations here. However, in most cases you should avoid changing state inside the hook. To react to state changes, it's usually better to use a [computed property](#computed) or [watcher](#watch) instead.
774
785
786
+
Note that `updated` does **not** guarantee that all child components have also been re-rendered. If you want to wait until the entire view has been re-rendered, you can use [vm.$nextTick](#vm-nextTick) inside of `updated`:
787
+
788
+
``` js
789
+
updated:function () {
790
+
this.$nextTick(function () {
791
+
// Code that will run only after the
792
+
// entire view has been re-rendered
793
+
})
794
+
}
795
+
```
796
+
775
797
**This hook is not called during server-side rendering.**
Copy file name to clipboardExpand all lines: src/v2/guide/comparison.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Comparison with Other Frameworks
3
3
type: guide
4
-
order: 29
4
+
order: 701
5
5
---
6
6
7
7
This is definitely the most difficult page in the guide to write, but we do feel it's important. Odds are, you've had problems you tried to solve and you've used another library to solve them. You're here because you want to know if Vue can solve your specific problems better. That's what we hope to answer for you.
Copy file name to clipboardExpand all lines: src/v2/guide/computed.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -68,9 +68,9 @@ var vm = new Vue({
68
68
Here we have declared a computed property `reversedMessage`. The function we provided will be used as the getter function for the property `vm.reversedMessage`:
69
69
70
70
```js
71
-
console.log(vm.reversedMessage) //-> 'olleH'
71
+
console.log(vm.reversedMessage) //=> 'olleH'
72
72
vm.message='Goodbye'
73
-
console.log(vm.reversedMessage) //-> 'eybdooG'
73
+
console.log(vm.reversedMessage) //=> 'eybdooG'
74
74
```
75
75
76
76
You can open the console and play with the example vm yourself. The value of `vm.reversedMessage` is always dependent on the value of `vm.message`.
Vue.js allows you to define filters that can be used to apply common text formatting. Filters are usable in two places: **mustache interpolations and `v-bind` expressions** (the latter supported in 2.1.0+). Filters should be appended to the end of the JavaScript expression, denoted by the "pipe" symbol:
8
+
9
+
```html
10
+
<!-- in mustaches -->
11
+
{{ message | capitalize }}
12
+
13
+
<!-- in v-bind -->
14
+
<divv-bind:id="rawId | formatId"></div>
15
+
```
16
+
17
+
The filter function always receives the expression's value (the result of the former chain) as its first argument. In this example, the `capitalize` filter function will receive the value of `message` as its argument.
In this case, `filterA`, defined with a single argument, will receive the value of `message`, and then the `filterB` function will be called with the result of `filterA` passed into `filterB`'s single argument.
39
+
40
+
Filters are JavaScript functions, therefore they can take arguments:
41
+
42
+
```html
43
+
{{ message | filterA('arg1', arg2) }}
44
+
```
45
+
46
+
Here `filterA` is defined as a function taking three arguments. The value of `message` will be passed into the first argument. The plain string `'arg1'` will be passed into the `filterA` as its second argument, and the value of expression `arg2` will be evaluated and passed in as the third argument.
Copy file name to clipboardExpand all lines: src/v2/guide/installation.md
+4
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,10 @@ Vue does **not** support IE8 and below, because it uses ECMAScript 5 features th
17
17
18
18
Detailed release notes for each version are available on [GitHub](https://github.com/vuejs/vue/releases).
19
19
20
+
## Vue Devtools
21
+
22
+
When using Vue, we recommend also installing the [Vue Devtools](https://github.com/vuejs/vue-devtools#vue-devtools) in your browser, allowing you to inspect and debug your Vue applications in a more user-friendly interface.
23
+
20
24
## Direct `<script>` Include
21
25
22
26
Simply download and include with a script tag. `Vue` will be registered as a global variable.
Copy file name to clipboardExpand all lines: src/v2/guide/instance.md
+58-34
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@ type: guide
4
4
order: 3
5
5
---
6
6
7
-
## Constructor
7
+
## Creating a Vue Instance
8
8
9
-
Every Vue application is bootstrapped by creating a **root Vue instance** with the `Vue` constructor function:
9
+
Every Vue application starts by creating a new **Vue instance** with the `Vue` function:
10
10
11
11
```js
12
12
var vm =newVue({
@@ -16,46 +16,68 @@ var vm = new Vue({
16
16
17
17
Although not strictly associated with the [MVVM pattern](https://en.wikipedia.org/wiki/Model_View_ViewModel), Vue's design was partly inspired by it. As a convention, we often use the variable `vm` (short for ViewModel) to refer to our Vue instance.
18
18
19
-
When you instantiate a Vue instance, you need to pass in an **options object** which can contain options for data, template, element to mount on, methods, lifecycle callbacks, and more. The full list of options can be found in the [API reference](../api/#Options-Data).
19
+
When you create a Vue instance, you pass in an **options object**. The majority of this guide describes how you can use these options to create your desired behavior. For reference, you can also browse the full list of options in the [API reference](../api/#Options-Data).
20
20
21
-
The `Vue` constructor can be extended to create reusable **component constructors** with pre-defined options:
21
+
A Vue application consists of a **root Vue instance** created with `new Vue`, optionally organized into a tree of nested, reusable components. For example, a todo app's component tree might look like this:
22
22
23
-
```js
24
-
var MyComponent =Vue.extend({
25
-
// extension options
26
-
})
27
-
28
-
// all instances of `MyComponent` are created with
29
-
// the pre-defined extension options
30
-
var myComponentInstance =newMyComponent()
23
+
```
24
+
Root Instance
25
+
|- TodoList
26
+
|- TodoItem
27
+
|- DeleteTodoButton
28
+
|- EditTodoButton
29
+
|- TodoListFooter
30
+
|- ClearTodosButton
31
+
|- TodoListStatistics
31
32
```
32
33
33
-
Although it is possible to create extended instances imperatively, most of the time it is recommended to compose them declaratively in templates as custom elements. We will talk about [the component system](components.html) in detail later. For now, you just need to know that all Vue components are essentially extended Vue instances.
34
+
We'll talk about [the component system](components.html) in detail later. For now, just know that all Vue components are also Vue instances, and so accept the same options object (except for a few root-specific options).
34
35
35
-
## Properties and Methods
36
+
## Data and Methods
36
37
37
-
Each Vue instance **proxies**all the properties found in its `data` object:
38
+
When a Vue instance is created, it adds all the properties found in its `data` object to Vue's **reactivity system**. When the values of those properties change, the view will "react", updating to match the new values.
38
39
39
40
```js
41
+
// Our data object
40
42
var data = { a:1 }
43
+
44
+
// The object is added to a Vue instance
41
45
var vm =newVue({
42
46
data: data
43
47
})
44
48
45
-
vm.a===data.a// -> true
49
+
// These reference the same object!
50
+
vm.a===data.a// => true
46
51
47
-
// setting the property also affects original data
52
+
// Setting the property on the instance
53
+
// also affects the original data
48
54
vm.a=2
49
-
data.a//-> 2
55
+
data.a//=> 2
50
56
51
57
// ... and vice-versa
52
58
data.a=3
53
-
vm.a// -> 3
59
+
vm.a// => 3
60
+
```
61
+
62
+
When this data changes, the view will re-render. It should be noted that properties in `data` are only **reactive** if they existed when the instance was created. That means if you add a new property, like:
63
+
64
+
```js
65
+
vm.b='hi'
54
66
```
55
67
56
-
It should be noted that only these proxied properties are **reactive**, meaning any changes to their values will trigger the view to re-render. If you attach a new property to the instance after it has been created, it will not trigger any view updates. We will discuss the reactivity system in detail later.
68
+
Then changes to `b` will not trigger any view updates. If you know you'll need a property later, but it starts out empty or non-existent, you'll just need to set some initial value. For example:
69
+
70
+
```js
71
+
data: {
72
+
newTodoText:'',
73
+
visitCount:0,
74
+
hideCompletedTodos:false,
75
+
todos: [],
76
+
error:null
77
+
}
78
+
```
57
79
58
-
In addition to data properties, Vue instances expose a number of useful instance properties and methods. These properties and methods are prefixed with `$` to differentiate them from proxied data properties. For example:
80
+
In addition to data properties, Vue instances expose a number of useful instance properties and methods. These are prefixed with `$` to differentiate them from user-defined properties. For example:
//this callback will be called when `vm.a` changes
93
+
vm.$watch('a', function (newValue, oldValue) {
94
+
//This callback will be called when `vm.a` changes
73
95
})
74
96
```
75
97
76
-
<pclass="tip">Don't use [arrow functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) on an instance property or callback (e.g. `vm.$watch('a', newVal => this.myMethod())`). As arrow functions are bound to the parent context, `this` will not be the Vue instance as you'd expect and `this.myMethod` will be undefined.</p>
77
-
78
-
Consult the [API reference](../api/#Instance-Properties) for the full list of instance properties and methods.
98
+
In the future, you can consult the [API reference](../api/#Instance-Properties) for a full list of instance properties and methods.
79
99
80
100
## Instance Lifecycle Hooks
81
101
82
-
Each Vue instance goes through a series of initialization steps when it is created - for example, it needs to set up data observation, compile the template, mount the instance to the DOM, and update the DOM when data changes. Along the way, it will also invoke some **lifecycle hooks**, which give us the opportunity to execute custom logic. For example, the [`created`](../api/#created) hook is called after the instance is created:
102
+
Each Vue instance goes through a series of initialization steps when it's created - for example, it needs to set up data observation, compile the template, mount the instance to the DOM, and update the DOM when data changes. Along the way, it also runs functions called **lifecycle hooks**, giving users the opportunity to add their own code at specific stages.
103
+
104
+
For example, the [`created`](../api/#created) hook can be used to run code after an instance is created:
83
105
84
106
```js
85
-
var vm =newVue({
107
+
newVue({
86
108
data: {
87
109
a:1
88
110
},
@@ -91,13 +113,15 @@ var vm = new Vue({
91
113
console.log('a is: '+this.a)
92
114
}
93
115
})
94
-
//-> "a is: 1"
116
+
//=> "a is: 1"
95
117
```
96
118
97
-
There are also other hooks which will be called at different stages of the instance's lifecycle, for example [`mounted`](../api/#mounted), [`updated`](../api/#updated), and [`destroyed`](../api/#destroyed). All lifecycle hooks are called with their `this` context pointing to the Vue instance invoking it. You may have been wondering where the concept of "controllers" lives in the Vue world and the answer is: there are no controllers. Your custom logic for a component would be split among these lifecycle hooks.
119
+
There are also other hooks which will be called at different stages of the instance's lifecycle, such as [`mounted`](../api/#mounted), [`updated`](../api/#updated), and [`destroyed`](../api/#destroyed). All lifecycle hooks are called with their `this` context pointing to the Vue instance invoking it.
120
+
121
+
<pclass="tip">Don't use [arrow functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) on an options property or callback, such as `created: () => console.log(this.a)` or `vm.$watch('a', newValue => this.myMethod())`. Since arrow functions are bound to the parent context, `this` will not be the Vue instance as you'd expect and `this.a` or `this.myMethod` will be undefined.</p>
98
122
99
123
## Lifecycle Diagram
100
124
101
-
Below is a diagram for the instance lifecycle. You don't need to fully understand everything going on right now, but this diagram will be helpful in the future.
125
+
Below is a diagram for the instance lifecycle. You don't need to fully understand everything going on right now, but as you learn and build more, it will be a useful reference.
0 commit comments