Skip to content

Commit 65cbf74

Browse files
committed
prop assertions
1 parent f32499b commit 65cbf74

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

source/guide/components.md

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,6 @@ new Vue({
140140

141141
<p class="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>
142142

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-
<child msg="{{parentMsg}}"></child>
152-
<!-- explicit two-way binding -->
153-
<child msg="{{@ parentMsg}}"></child>
154-
<!-- explicit one-time binding -->
155-
<child msg="{{* 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-
<p class="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-
162143
### Passing Callbacks as Props
163144

164145
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', {
188169
<child on-load="{{onChildLoaded}}"></child>
189170
```
190171

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+
<child msg="{{parentMsg}}"></child>
181+
<!-- explicit two-way binding -->
182+
<child msg="{{@ parentMsg}}"></child>
183+
<!-- explicit one-time binding -->
184+
<child msg="{{* 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+
<p class="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+
191235
### Inheriting Parent Scope
192236

193237
If you want, you can also use the `inherit: true` option for your child component to make it prototypally inherit all parent properties:

themes/vue/source/css/page.styl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ $header-height = 40px
175175
p, ul, ol
176176
line-height 1.6em
177177
ul, ol
178-
padding-left 1em
178+
padding-left 1.5em
179179
a
180180
color $green
181181
font-weight 600

0 commit comments

Comments
 (0)