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 component props to introduce prop types earlier, fixes#1644 (#1646)
@BlitZz96 brought up a good point in [this issue](#1644) about users lacking the necessary background knowledge to fully understand Passing Static and Dynamic Props, at least as it relates to booleans and potentially other types in the future. To fix this, I added a new Prop Types section right before it.
Usually though, you'll want every prop to be a specific type of value. In these cases, you can list props as an object, where the properties' names and values contain the prop names and types, respectively:
37
+
38
+
```js
39
+
props: {
40
+
title:String,
41
+
likes:Number,
42
+
isPublished:Boolean,
43
+
commentIds:Array,
44
+
author:Object
45
+
}
46
+
```
47
+
48
+
This not only documents your component, but will also warn users in the browser's JavaScript console if they pass the wrong type. You'll learn much more about [type checks and other prop validations](#Prop-Validation) further down this page.
49
+
50
+
## Passing Static or Dynamic Props
29
51
30
52
So far, you've seen props passed a static value, like in:
31
53
@@ -36,7 +58,11 @@ So far, you've seen props passed a static value, like in:
36
58
You've also seen props assigned dynamically with `v-bind`, such as in:
37
59
38
60
```html
61
+
<!-- Dynamically assign the value of a variable -->
39
62
<blog-postv-bind:title="post.title"></blog-post>
63
+
64
+
<!-- Dynamically assign the value of a complex expression -->
65
+
<blog-postv-bind:title="post.title + ' by ' + post.author.name"></blog-post>
40
66
```
41
67
42
68
In the two examples above, we happen to pass string values, but _any_ type of value can actually be passed to a prop.
@@ -56,14 +82,14 @@ In the two examples above, we happen to pass string values, but _any_ type of va
56
82
57
83
```html
58
84
<!-- Including the prop with no value will imply `true`. -->
59
-
<blog-postfavorited></blog-post>
85
+
<blog-postis-published></blog-post>
60
86
61
87
<!-- Even though `false` is static, we need v-bind to tell Vue that -->
62
88
<!-- this is a JavaScript expression rather than a string. -->
@@ -148,7 +174,7 @@ There are usually two cases where it's tempting to mutate a prop:
148
174
149
175
## Prop Validation
150
176
151
-
Components can specify requirements for its props. If a requirement isn't met, Vue will warn you in the browser's JavaScript console. This is especially useful when developing a component that's intended to be used by others.
177
+
Components can specify requirements for its props, such as the types you've already seen. If a requirement isn't met, Vue will warn you in the browser's JavaScript console. This is especially useful when developing a component that's intended to be used by others.
152
178
153
179
To specify prop validations, you can provide an object with validation requirements to the value of `props`, instead of an array of strings. For example:
154
180
@@ -200,9 +226,10 @@ The `type` can be one of the following native constructors:
200
226
- String
201
227
- Number
202
228
- Boolean
203
-
- Function
204
-
- Object
205
229
- Array
230
+
- Object
231
+
- Date
232
+
- Function
206
233
- Symbol
207
234
208
235
In addition, `type` can also be a custom constructor function and the assertion will be made with an `instanceof` check. For example, given the following constructor function exists:
0 commit comments