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: source/api/directives.md
+77-58Lines changed: 77 additions & 58 deletions
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,9 @@ type: api
3
3
order: 6
4
4
---
5
5
6
-
## Data Binding Directives
6
+
## Reactive Directives
7
7
8
-
> These directives can bind themselves to a property on the ViewModel, or to an expression which is evaluated in the context of the ViewModel. When the value of the underlying property or expression changes, the `update()` function of these directives will be called asynchronously on next tick.
8
+
> These directives can bind themselves to a property on the Vue instance, or to an expression which is evaluated in the context of the instance. When the value of the underlying property or expression changes, the `update()` function of these directives will be called asynchronously on next tick.
9
9
10
10
### v-text
11
11
@@ -35,9 +35,9 @@ If a directive argument is provided, the argument will be the class to be toggle
35
35
36
36
```html
37
37
<spanv-class="
38
-
red : hasError,
39
-
bold : isImportant,
40
-
hidden : isHidden
38
+
red : hasError,
39
+
bold : isImportant,
40
+
hidden : isHidden
41
41
"></span>
42
42
```
43
43
@@ -71,9 +71,9 @@ When there is an argument, it will be used as the CSS property to apply. Combine
71
71
72
72
```html
73
73
<divv-style="
74
-
top: top + 'px',
75
-
left: left + 'px',
76
-
background-color: 'rgb(0,0,' + bg + ')'
74
+
top: top + 'px',
75
+
left: left + 'px',
76
+
background-color: 'rgb(0,0,' + bg + ')'
77
77
"></div>
78
78
```
79
79
@@ -88,29 +88,48 @@ When the argument is prefixed with `$`, Vue.js will automatically apply prefixed
88
88
### v-on
89
89
90
90
- This directive requires an argument.
91
-
- This directive requires the value to be a Function or an expression.
91
+
- This directive requires the value to be a Function or a statement.
92
92
93
93
Attaches an event listener to the element. The event type is denoted by the argument. It is also the only directive that can be used with the `key` filter. For more details see [Listening for Events](/guide/events.html).
94
94
95
95
### v-model
96
96
97
-
- This directive can only be used on INPUT, SELECT, TEXTAREA and elements with `contenteditable` attribute.
97
+
- This directive can only be used on `<input>`, `<select>` or `<textarea>` elements.
98
+
- Directive params: `lazy`, `number`, `options`
98
99
99
-
Create a two-way binding on a form or editable element. Data is synced on every `input` event by default. When the ViewModel has the `lazy` option set to true, data will be synced only on `change` events. For more details see [Handling Forms](/guide/forms.html).
100
+
Create a two-way binding on a form input element. Data is synced on every `input` event by default. For detailed examples see [Handling Forms](/guide/forms.html).
100
101
101
102
### v-if
102
103
103
-
- This directive creates child ViewModels.
104
104
- This directive can trigger transitions.
105
105
106
-
Conditionally insert / remove the element based on the truthy-ness of the binding value. A child ViewModel will be created on the bound element. It is instantiated when the value is truthy, and destroyed when the value becomes falsy. If the binding starts with a falsy value, the ViewModel will not be instantiated until the value actually becomes truthy.
106
+
Conditionally insert / remove the element based on the truthy-ness of the binding value. If the element is a `<template>` element, its content will be extracted as the conditional block.
107
+
108
+
**Example:**
109
+
110
+
```html
111
+
<templatev-if="test">
112
+
<p>hello</p>
113
+
<p>world</p>
114
+
</template>
115
+
```
116
+
117
+
Will render:
118
+
119
+
```html
120
+
<!--v-if-start-->
121
+
<p>hello</p>
122
+
<p>world</p>
123
+
<!--v-if-end-->
124
+
```
107
125
108
126
### v-repeat
109
127
110
-
- This directive creates child ViewModels.
128
+
- This directive creates child Vue instances.
111
129
- This directive requires the value to be an Array or Object.
112
130
- This directive can trigger transitions.
113
131
- This directive accepts an optional argument.
132
+
- Directive params: `trackby`
114
133
115
134
Create a child ViewModel for every item in the binding Array. These child ViewModels will be automatically created / destroyed when mutating methods, e.g. `push()`, are called on the Array.
116
135
@@ -120,64 +139,59 @@ When no argument is provided, the child ViewModel will directly use the assigned
120
139
121
140
```html
122
141
<ul>
123
-
<liv-repeat="users">
124
-
{{name}} {{email}}
125
-
</li>
142
+
<liv-repeat="users">
143
+
{{name}} {{email}}
144
+
</li>
126
145
</ul>
127
146
```
128
147
129
148
If an argument is provided, a wrapper data object will always be created, using the argument string as the alias key. This allows for more explicit property access in templates:
130
149
131
150
```html
132
151
<ul>
133
-
<liv-repeat="user : users">
134
-
{{user.name}} {{user.email}}
135
-
</li>
152
+
<liv-repeat="user : users">
153
+
{{user.name}} {{user.email}}
154
+
</li>
136
155
</ul>
137
156
```
138
157
139
158
For detailed examples, see [Displaying a List](/guide/list.html).
140
159
141
-
### v-view
142
-
143
-
- This directive creates child ViewModels.
144
-
- This directive can trigger transitions.
145
-
146
-
Conditionally instantiate ViewModels, using the bound value as the Component ID to look up constructors with. When the bound value changes, existing ViewModel will be destroyed and a new ViewModel will be created. When a ViewModel is created, the original element will be replaced, but all attributes will be copied to the new element. For more details, see [Routing](/guide/application.html#Routing).
147
-
148
160
### v-with
149
161
150
-
- This directive must be used with another directive that creates child ViewModels.
162
+
- This directive can only be used with `v-component`.
151
163
- This directive accepts only keypaths, no expressions.
152
164
153
-
Allows a child ViewModel to inherit data from the parents. You can either pass in an Object which will be used as the `data` option, or bind individual parent properties to the child with different keys. This directive must be used in combination with another directive that creates a child ViewModel, e.g. `v-component` or `v-view`.
165
+
Allows a child ViewModel to inherit data from the parents. You can either pass in an Object which will be used as the `data` option, or bind individual parent properties to the child with different keys. This directive must be used in combination with `v-component`.
166
+
167
+
The data inheritance is one-way: when parent property changes, the child will be notified of the change and update accordingly; however if the child sets the property to something else, it will not affect the parent, and the modified property will be overwritten the next time parent property changes.
154
168
155
169
Example inheriting an object:
156
170
157
171
```js
158
172
// parent data looks like this
159
173
{
160
-
user: {
161
-
name:'Foo Bar',
162
-
email:'foo@bar.com'
163
-
}
174
+
user: {
175
+
name:'Foo Bar',
176
+
email:'foo@bar.com'
177
+
}
164
178
}
165
179
```
166
180
167
181
```html
168
-
<divv-with="user">
169
-
<!-- you can access properties without `user.` -->
170
-
{{name}} {{email}}
171
-
</div>
182
+
<my-componentv-with="user">
183
+
<!-- you can access properties without `user.` -->
184
+
{{name}} {{email}}
185
+
</my-component>
172
186
```
173
187
174
188
Example inheriting individual properties (using the same data):
<!-- you can access properties with the new keys -->
193
+
{{myName}} {{myEmail}}
194
+
</my-component>
181
195
```
182
196
183
197
## Literal Directives
@@ -186,41 +200,46 @@ Example inheriting individual properties (using the same data):
186
200
187
201
### v-component
188
202
203
+
- Directive params: `keep-alive`
204
+
189
205
Compile this element as a child ViewModel with a registered component constructor. This can be used with `v-with` to inehrit data from the parent. For more details see [Component System](/guide/components.html).
190
206
191
207
### v-ref
192
208
193
-
Only respected when used in combination with directives that create child components: `v-component`, `v-view`, `v-if`or `v-repeat`. The ViewModel will be accessible in its parent's `$` object, e.g. `parent.$[id]`. When used with `v-repeat`, the value will be an Array containing the child ViewModel instances corresponding to the Array they are bound to. For examples see [Accessing Child Components](/guide/components.html#Accessing_Child_Components).
209
+
Register a reference to a child component on its parent for easier access. Only respected when used in combination with `v-component`or `v-repeat`. The component instance will be accessible on its parent's `$` object. For an example, see [child reference](/guide/components.html#Child_Reference).
194
210
195
-
### v-partial
211
+
When used with `v-repeat`, the value will be an Array containing all the child Vue instances corresponding to the Array they are bound to.
196
212
197
-
Replace the element's innerHTML with a registered partial. Partials can be registered with `Vue.partial()` or passed inside the `partials` option. You can also use this syntax (which doesn't support expressions):
213
+
### v-el
198
214
199
-
```html
200
-
<div>{{> my-partial}}</div>
201
-
```
215
+
Register a reference to a DOM element on its owner Vue instance for easier access. e.g. `<div v-el="hi">` will be accessible as `vm.$$.hi`.
202
216
203
-
### v-effect
217
+
### v-partial
204
218
205
-
Apply a registered JavaScript effect to the element. JavaScript effects can be registered with `Vue.effect()` or passed inside the `effects` option.
219
+
Replace the element's innerHTML with a registered partial. Partials can be registered with `Vue.partial()` or passed inside the `partials` option.
206
220
207
-
For details, see [the guide](/guide/transitions.html#JavaScript_Functions).
221
+
Using the mustache tag inside `v-partial` makes it reactive:
208
222
209
-
## Empty Directives
223
+
```html
224
+
<!-- content will change based on vm.partialId -->
225
+
<divv-partial="{{partialId}}"></div>
226
+
```
210
227
211
-
> Empty directives do not require and will ignore their attribute value.
228
+
You can also use this syntax (which doesn't support reactivity):
212
229
213
-
### v-transition
230
+
```html
231
+
<div>{{> my-partial}}</div>
232
+
```
214
233
215
-
Notify Vue.js to apply transition CSS classes to this element. The transition classes are applied when certain transition-triggering directives modify the element, or when the ViewModel's DOM manipulation methods are called.
234
+
### v-transition
216
235
217
-
For details, see [the guide](/guide/transitions.html#CSS_Transitions).
236
+
Notify Vue.js to apply transitions to this element. The transition classes are applied when certain transition-triggering directives modify the element, or when the Vue instance's DOM manipulation methods are called.
218
237
219
-
### v-animation
238
+
For details, see [the guide on transitions](/guide/transitions.html).
220
239
221
-
Notify Vue.js to apply animation CSS classes to this element. The order of CSS class application is different from `v-transition`.
240
+
## Empty Directives
222
241
223
-
For details, see [the guide](/guide/transitions.html#CSS_Animations).
242
+
> Empty directives do not require and will ignore their attribute value.
Copy file name to clipboardExpand all lines: source/api/options.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -292,7 +292,7 @@ parent.a // -> 2
292
292
293
293
### events
294
294
295
-
An object where keys are events to listen for and values are the corresponding callbacks. The value can also be a string of a method name. The Vue instance will call `$on()` for each entry in the object at instantiation.
295
+
An object where keys are events to listen for and values are the corresponding callbacks. Note these are Vue events rather than DOM events. The value can also be a string of a method name. The Vue instance will call `$on()` for each entry in the object at instantiation.
Copy file name to clipboardExpand all lines: source/guide/components.md
+32Lines changed: 32 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -62,6 +62,38 @@ It is important to understand the difference between `Vue.extend()` and `Vue.com
62
62
63
63
Vue.js supports two different API paradigms: the class-based, imperative, Backbone style API, and the markup-based, declarative, Web Components style API. If you are confused, think about how you can create an image element with `new Image()`, or with an `<img>` tag. Each is useful in its own right and Vue.js provides both for maximum flexibility.
64
64
65
+
## Dynamic Components
66
+
67
+
You can dynamically switch between components by using Mustache tags inside the `v-component` direcitve, which can be used together with routers to achieve "page switching":
68
+
69
+
```js
70
+
newVue({
71
+
el:'body',
72
+
data: {
73
+
currentView:'home'
74
+
},
75
+
components: {
76
+
home: { /* ... */ },
77
+
posts: { /* ... */ },
78
+
archive: { /* ... */ }
79
+
}
80
+
})
81
+
```
82
+
83
+
```html
84
+
<divv-component="{{currentView}}">
85
+
<!-- content changes when vm.currentview changes! -->
86
+
</div>
87
+
```
88
+
89
+
If you want to keep the switched-out components alive so that you can preserve its state or avoid re-rendering, you can add a `keep-alive` directive param:
Every component, or Vue instance, has its own lifecycle: it will be created, compiled, inserted or detached, and finally destroyed. At each of these key moments the instance will emit corresponding events, and when creating an instance or defining a component, we can pass in lifecycle hook functions to react to these events. For example:
Copy file name to clipboardExpand all lines: source/guide/forms.md
+17-8Lines changed: 17 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -77,6 +77,23 @@ new Vue({
77
77
})
78
78
</script>
79
79
80
+
## Lazy Updates
81
+
82
+
By default, `v-model` syncs the input with the data after each `input` event. You can add a `lazy` attribute to change the behavior to sync after `change` events:
83
+
84
+
```html
85
+
<!-- synced after "change" instead of "input" -->
86
+
<inputv-model="msg"lazy>
87
+
```
88
+
89
+
## Casting Value as Number
90
+
91
+
If you want user input to be automatically persisted as numbers, you can add a `number` attribute to your `v-model` managed inputs:
92
+
93
+
```html
94
+
<inputv-model="age"number>
95
+
```
96
+
80
97
## Dynamic Select Options
81
98
82
99
When you need to dynamically render a list of options for a `<select>` element, it's recommended to use an `options` attribute together with `v-model`:
@@ -129,12 +146,4 @@ Will render:
129
146
</select>
130
147
```
131
148
132
-
## Casting Value as Number
133
-
134
-
If you want user input to be automatically persisted as numbers, you can add a `number` attribute to your `v-model` managed inputs:
You might think this will blow away the existing DOM and re-build everything. But worry not - Vue.js recognizes array elements that already have an associated Vue instance and will reuse those instances whenever possible.
157
157
158
+
## Using `trackby`
159
+
158
160
In some cases, you might need to replace the Array with completely new objects - e.g. ones returned from an API call. If your data objects have a unique id property, then you can use a `trackby` attribute to give Vue.js a hint so that it can reuse an existing instance with data that has the same id.
0 commit comments