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
@@ -62,89 +62,15 @@ 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:
100
-
101
-
```js
102
-
var MyComponent =Vue.extend({
103
-
created:function () {
104
-
console.log('An instance of MyComponent has been created!')
105
-
}
106
-
})
107
-
```
108
-
109
-
Check out the API reference for a [full list of lifecycle hooks](/api/options.html#Lifecycle) that are availble.
110
-
111
65
## Data Inheritance
112
66
113
-
### Scope Inheritance
114
-
115
-
By default, components have **isolated scope**. This means you cannot reference parent data in a child component's template. If you want though, you can use the `inherit: true` option for your child component to make it prototypally inherit parent properties:
116
-
117
-
```js
118
-
var parent =newVue({
119
-
data: {
120
-
a:1
121
-
}
122
-
})
123
-
// $addChild() is an instance method that allows you to
124
-
// programatically create a child instance.
125
-
var child =parent.$addChild({
126
-
inherit:true,
127
-
data: {
128
-
b:2
129
-
}
130
-
})
131
-
console.log(child.a) // -> 1
132
-
console.log(child.b) // -> 2
133
-
parent.a=3
134
-
console.log(child.a) // -> 3
135
-
```
136
-
137
-
Note this comes with a caveat: because data properties on Vue instances are getter/setters, setting `child.a = 2` will change `parent.a` instead of creating a new property on the child shadowing the parent one:
By default, components have **isolated scope**. This means you cannot reference parent data in a child component's template. To explicitly pass data to child components with isolated scope, we can use the `v-with` directive.
144
70
145
-
###Explicit Data Passing
71
+
#### Passing Down Child `$data`
146
72
147
-
To explicitly pass data to child components with isolated scope, we can use the `v-with` directive. When given a single keypath without an argument, the corresponding value on the parent will be passed down to the child as its `$data`. This means the passed-down value must be an object, and it will overwrite the default `$data` object the child component might have.
73
+
When given a single keypath without an argument, the corresponding value on the parent will be passed down to the child as its `$data`. This means the passed-down value must be an object, and it will overwrite the default `$data` object the child component might have.
148
74
149
75
**Example:**
150
76
@@ -190,12 +116,14 @@ var parent = new Vue({
190
116
})
191
117
</script>
192
118
119
+
#### Passing Down Individual Properties
120
+
193
121
`v-with` can also be used with an argument in the form of `v-with="childProp: parentProp"`. This means passing down `parent[parentProp]` to the child as `child[childProp]`. Note this data inheritance is one-way: when `parentProp` changes, `childProp` will be updated accordingly, however not the other way around.
It is also possible to use the [`paramAttributes`](/api/options.html#paramAttributes) option, which compiles into `v-with`, to expose an interface that looks more like custom elements:
168
+
169
+
```html
170
+
<divid="demo-3">
171
+
<inputv-model="parentMsg">
172
+
<child-componentchild-msg="parentMsg"></p>
173
+
</div>
174
+
```
175
+
176
+
```js
177
+
newVue({
178
+
el:'#demo-3',
179
+
data: {
180
+
parentMsg:'Inherited message'
181
+
},
182
+
components: {
183
+
'child-component': {
184
+
paramAttributes: ['child-msg'],
185
+
// dashed attributes are camelized,
186
+
// so 'child-msg' becomes 'this.childMsg'
187
+
template:'<span>{{childMsg}}</span>'
188
+
}
189
+
}
190
+
})
191
+
```
192
+
193
+
### Scope Inheritance
194
+
195
+
If you want, you can also use the `inherit: true` option for your child component to make it prototypally inherit all parent properties:
196
+
197
+
```js
198
+
var parent =newVue({
199
+
data: {
200
+
a:1
201
+
}
202
+
})
203
+
// $addChild() is an instance method that allows you to
204
+
// programatically create a child instance.
205
+
var child =parent.$addChild({
206
+
inherit:true,
207
+
data: {
208
+
b:2
209
+
}
210
+
})
211
+
console.log(child.a) // -> 1
212
+
console.log(child.b) // -> 2
213
+
parent.a=3
214
+
console.log(child.a) // -> 3
215
+
```
216
+
217
+
Note this comes with a caveat: because data properties on Vue instances are getter/setters, setting `child.a = 2` will change `parent.a` instead of creating a new property on the child shadowing the parent one:
The directives here (`v-show` and `v-on`) will be compiled in the parent's scope, so the value of `active` and `onClick` will be resolved against the parent. Any directives/interpolations inside the child's template will be compiled in the child's scope. This ensures a cleaner separation between parent and child components.
235
+
236
+
This rule also applies to [content insertion](#Content_Insertion), as explained later in this guide.
237
+
238
+
## Component Lifecycle
239
+
240
+
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:
241
+
242
+
```js
243
+
var MyComponent =Vue.extend({
244
+
created:function () {
245
+
console.log('An instance of MyComponent has been created!')
246
+
}
247
+
})
248
+
```
249
+
250
+
Check out the API reference for a [full list of lifecycle hooks](/api/options.html#Lifecycle) that are availble.
251
+
252
+
## Dynamic Components
253
+
254
+
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":
255
+
256
+
```js
257
+
newVue({
258
+
el:'body',
259
+
data: {
260
+
currentView:'home'
261
+
},
262
+
components: {
263
+
home: { /* ... */ },
264
+
posts: { /* ... */ },
265
+
archive: { /* ... */ }
266
+
}
267
+
})
268
+
```
269
+
270
+
```html
271
+
<divv-component="{{currentView}}">
272
+
<!-- content changes when vm.currentview changes! -->
273
+
</div>
274
+
```
275
+
276
+
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:
For an Array of Objects, you can combine `v-component` with `v-repeat`. In this case, for each Object in the Array, a child ViewModel will be created using that Object as data, and the specified component as the constructor.
240
287
@@ -391,6 +438,8 @@ MyComponent
391
438
392
439
When creating reusable components, we often need to access and reuse the original content in the hosting element, which are not part of the component (similar to the Angular concept of "transclusion".) Vue.js implements a content insertion mechanism that is compatible with the current Web Components spec draft, using the special `<content>` element to serve as insertion points for the original content.
393
440
441
+
<pclass="tip">Note: "transcluded" contents are compiled in the parent component's scope.</p>
442
+
394
443
### Single Insertion Point
395
444
396
445
When there is only one `<content>` tag with no attributes, the entire original content will be inserted at its position in the DOM and replaces it. Anything originally inside the `<content>` tags is considered **fallback content**. Fallback content will only be displayed if the hosting element is empty and has no content to be inserted. For example:
0 commit comments