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/guide/components.md
+63-19Lines changed: 63 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -140,25 +140,6 @@ new Vue({
140
140
141
141
<pclass="tip">It is also possible to expose `$data` as a prop. The passed in value must be an Object and will replace the component's default `$data`.</p>
142
142
143
-
### Prop Binding Types
144
-
145
-
By default, all props form a **one-way-down** binding between the child property and the parent one: when the parent property updates, it will be synced down to the child, but not the other way around. This default is meant to prevent child components from accidentally mutating the parent's state, which can make your app's data flow harder to reason about. However, it is also possible to explicitly enforce a two-way or a one-time binding:
146
-
147
-
Compare the syntax:
148
-
149
-
```html
150
-
<!-- default, one-way-down binding -->
151
-
<childmsg="{{parentMsg}}"></child>
152
-
<!-- explicit two-way binding -->
153
-
<childmsg="{{@ parentMsg}}"></child>
154
-
<!-- explicit one-time binding -->
155
-
<childmsg="{{* parentMsg}}"></child>
156
-
```
157
-
158
-
The two-way binding will sync the change of child's `msg` property back to the parent's `parentMsg` property. The one-time binding, once set up, will not sync future changes between the the parent and the child.
159
-
160
-
<pclass="tip">Note that if the prop being passed down is an Object or an Array, it is passed by reference. Mutating the Object or Array itself inside the child will affect parent state, regardless of the binding type you are using.</p>
161
-
162
143
### Passing Callbacks as Props
163
144
164
145
It is also possible to pass down a method or a statement as a callback to a child component. This enables declarative, decoupled parent-child communication:
@@ -188,6 +169,69 @@ Vue.component('child', {
188
169
<childon-load="{{onChildLoaded}}"></child>
189
170
```
190
171
172
+
### Prop Binding Types
173
+
174
+
By default, all props form a **one-way-down** binding between the child property and the parent one: when the parent property updates, it will be synced down to the child, but not the other way around. This default is meant to prevent child components from accidentally mutating the parent's state, which can make your app's data flow harder to reason about. However, it is also possible to explicitly enforce a two-way or a one-time binding:
175
+
176
+
Compare the syntax:
177
+
178
+
```html
179
+
<!-- default, one-way-down binding -->
180
+
<childmsg="{{parentMsg}}"></child>
181
+
<!-- explicit two-way binding -->
182
+
<childmsg="{{@ parentMsg}}"></child>
183
+
<!-- explicit one-time binding -->
184
+
<childmsg="{{* parentMsg}}"></child>
185
+
```
186
+
187
+
The two-way binding will sync the change of child's `msg` property back to the parent's `parentMsg` property. The one-time binding, once set up, will not sync future changes between the the parent and the child.
188
+
189
+
<pclass="tip">Note that if the prop being passed down is an Object or an Array, it is passed by reference. Mutating the Object or Array itself inside the child will affect parent state, regardless of the binding type you are using.</p>
190
+
191
+
### Prop Validation
192
+
193
+
It is possible for a component to validate the props it is receiving. This is useful when you are authoring a component that is intended to be used by others, as these prop validation requirements essentially constitute your component's API, and ensure your users are using your component correctly. Instead of defining the props as strings, you can use Objects that contain validation requirements:
194
+
195
+
```js
196
+
Vue.component('example', {
197
+
props: [
198
+
// type check
199
+
{
200
+
name:'on-something',
201
+
type:Function
202
+
},
203
+
// presence check
204
+
{
205
+
name:'required-prop',
206
+
type:String,
207
+
required:true
208
+
},
209
+
// custom validator function
210
+
{
211
+
name:'greater-than-ten',
212
+
validator:function (value) {
213
+
return value >10
214
+
}
215
+
}
216
+
]
217
+
})
218
+
```
219
+
220
+
The `type` can be one of the following native constructors:
221
+
222
+
- String
223
+
- Number
224
+
- Boolean
225
+
- Function
226
+
- Object
227
+
- Array
228
+
229
+
In addition, `type` can also be a custom constructor function and the the assertion will be made with an `instanceof` check.
230
+
231
+
When a prop validation fails, Vue will refuse the set the value on the child component, and throw a warning if using the development build.
232
+
233
+
You can still use strings if your props don't need any validation, and you can mix string and object props in the option array.
234
+
191
235
### Inheriting Parent Scope
192
236
193
237
If you want, you can also use the `inherit: true` option for your child component to make it prototypally inherit all parent properties:
0 commit comments