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
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:
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.
If you want to pass all the properties of an object as props, you can use `v-bind` without an argument (`v-bind` instead of `:prop-name`). For example, given a `post` object:
All props form a **one-way-down binding**between the child property and the parent one: when the parent property updates, it will flow down to the child, but not the other way around. This prevents child components from accidentally mutating the parent's state, which can make your app's data flow harder to understand.
In addition, every time the parent component is updated, all props in the child component will be refreshed with the latest value. This means you should **not**attempt to mutate a prop inside a child component. If you do, Vue will warn you in the console.
There are usually two cases where it's tempting to mutate a prop:
131
+
プロパティの値を変化させたい場合は主に 2 つあります:
131
132
132
-
1.**The prop is used to pass in an initial value; the child component wants to use it as a local data property afterwards.**In this case, it's best to define a local data property that uses the prop as its initial value:
133
+
1.**プロパティを初期値として受け渡し、子コンポーネントにてローカルのデータとして後で利用したいと考える場合。**この場合は、プロパティの値を初期値として使うローカルの data プロパティを定義するのがベストです:
133
134
134
135
```js
135
136
props: ['initialCounter'],
@@ -140,7 +141,7 @@ data() {
140
141
}
141
142
```
142
143
143
-
2.**The prop is passed in as a raw value that needs to be transformed.**In this case, it's best to define a computed property using the prop's value:
Note that objects and arrays in JavaScript are passed by reference, so if the prop is an array or object, mutating the object or array itself inside the child component **will**affect parent state.
Components can specify requirements for their 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.
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:
Note that props are validated **before**a component instance is created, so instance properties (e.g.`data`, `computed`, etc) will not be available inside `default` or`validator`functions.
The `type`can be one of the following native constructors:
219
+
`type`は、次のネイティブコンストラクタのいずれかです:
218
220
219
221
- String
220
222
- Number
@@ -225,7 +227,7 @@ The `type` can be one of the following native constructors:
225
227
- Function
226
228
- Symbol
227
229
228
-
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:
HTML の属性名は大文字小文字を区別しないので、ブラウザは全ての大文字を小文字として解釈します。つまり、 DOM(HTML) テンプレート内においては、キャメルケースのプロパティ名はケバブケース(ハイフンで区切ったもの)を使う必要があります。
250
254
251
-
HTML attribute names are case-insensitive, so browsers will interpret any uppercase characters as lowercase. That means when you're using in-DOM templates, camelCased prop names need to use their kebab-cased (hyphen-delimited) equivalents:
252
255
253
256
```js
254
257
constapp=Vue.createApp({})
255
258
256
259
app.component('blog-post', {
257
-
//camelCase in JavaScript
260
+
// JavaScript 内ではキャメルケース
258
261
props: ['postTitle'],
259
262
template:'<h3>{{ postTitle }}</h3>'
260
263
})
261
264
```
262
265
263
266
```html
264
-
<!--kebab-case in HTML -->
267
+
<!-- HTML 内ではケバブケース-->
265
268
<blog-postpost-title="hello!"></blog-post>
266
269
```
267
270
268
-
Again, if you're using string templates, this limitation does not apply.
0 commit comments