Skip to content

Commit c67403c

Browse files
authored
docs: #15 Custom Eventsの翻訳 (#64)
* #15 Custom Eventsの翻訳 * fix: apply feedback
1 parent 55d6c3e commit c67403c

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

src/guide/component-custom-events.md

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
1-
# Custom Events
1+
# カスタムイベント
22

3-
> This page assumes you've already read the [Components Basics](component-basics.md). Read that first if you are new to components.
3+
> このページは [コンポーネントの基本](component-basics.md) が読まれていることが前提となっています。 コンポーネントを扱った事のない場合はこちらのページを先に読んでください。
44
5-
## Event Names
5+
## イベント名
66

7-
Unlike components and props, event names don't provide any automatic case transformation. Instead, the name of an emitted event must exactly match the name used to listen to that event. For example, if emitting a camelCased event name:
7+
コンポーネントやプロパティとは違い、イベント名の大文字と小文字は自動的に変換されません。その代わり発火されるイベント名とイベントリスナ名は全く同じにする必要があります。例えばキャメルケース(camelCase)のイベント名でイベントを発火した場合:
88

99
```js
1010
this.$emit('myEvent')
1111
```
1212

13-
Listening to the kebab-cased version will have no effect:
13+
ケバブケース(kebab-case)でリスナ名を作っても何も起こりません:
1414

1515
```html
1616
<!-- Won't work -->
1717
<my-component @my-event="doSomething"></my-component>
1818
```
1919

20-
Since event names will never be used as variable or property names in JavaScript, there is no reason to use camelCase or PascalCase. Additionally, `v-on` event listeners inside DOM templates will be automatically transformed to lowercase (due to HTML's case-insensitivity), so `@myEvent` would become `@myevent` -- making `myEvent` impossible to listen to.
20+
イベント名は JavaScript 内で変数やプロパティ名として扱われることはないので、キャメルケース(camelCase)またはパスカルケース(PascalCase)を使用する理由はありません。 さらに DOM テンプレート内の `v-on` イベントリスナは自動的に小文字に変換されます ( HTML が大文字と小文字を判別しないため)。このため `@myEvent` `@myevent` になり `myEvent` にリスナが反応することができなくなります。
2121

22-
For these reasons, we recommend you **always use kebab-case for event names**.
22+
こういった理由から **いつもケバブケース(kebab-case)を使うこと** をお薦めします。
2323

24-
## Defining Custom Events
24+
## カスタムイベントの定義
2525

26-
Emitted events can be defined on the component via the `emits` option.
26+
発行されたイベントは、 `emits` オプションを介して、コンポーネントで定義することが出来ます。
2727

2828
```js
2929
app.component('custom-form', {
3030
emits: ['in-focus', 'submit']
3131
})
3232
```
3333

34-
In the event a native event (e.g., `click`) is defined in the `emits` option, it will be overwritten by the event in the component instead of being treated as a native listener.
34+
35+
ネイティブイベント(例、 `click` など)が `emits` オプションで定義されている場合、ネイティブリスナーとして扱われるのではなく、コンポーネントのイベントによって上書きされます。
3536

3637
::: tip
37-
It is recommended to define all emitted events in order to better document how a component should work.
38+
コンポーネントの動作を実証するために、全ての発行されたイベントを定義することをお勧めします。
3839
:::
3940

40-
### Validate Emitted Events
41+
### 発行されたイベントを検証する
4142

42-
Similar to prop type validation, an emitted event can be validated if it is defined with the Object syntax instead of the Array syntax.
43+
プロパティの型検証と同様に、発行されたイベントは、配列構文ではなくオブジェクト構文で定義されている場合に検証できます。
4344

44-
To add validation, the event is assigned a function that receives the arguments passed to the `$emit` call and returns a boolean to indicate whether the event is valid or not.
45+
検証を追加するために、イベントには、 `$emit` 呼び出しに渡された引数を受け取る関数が割り当てられ、イベントが有効かどうかを示す真偽値を返します。
4546

4647
```js
4748
app.component('custom-form', {
@@ -67,15 +68,14 @@ app.component('custom-form', {
6768
})
6869
```
6970

70-
## `v-model` arguments
71+
## `v-model` の引数
7172

72-
By default, `v-model` on a component uses `modelValue` as the prop and `update:modelValue` as the event. We can modify these names passing an argument to `v-model`:
73+
デフォルトでは、コンポーネントの `v-model` はプロパティとして `modelValue` を使用し、イベントとして `update:modelValue` を使用します。`v-model` 引数を渡してこれらの名前の変更が出来ます。
7374

7475
```html
7576
<my-component v-model:foo="bar"></my-component>
7677
```
77-
78-
In this case, child component will expect a `foo` prop and emits `update:foo` event to sync:
78+
この場合、子コンポーネントは `foo` プロパティを期待し、同期するために `update:foo` イベントを発行します。
7979

8080
```js
8181
const app = Vue.createApp({})
@@ -85,7 +85,7 @@ app.component('my-component', {
8585
foo: String
8686
},
8787
template: `
88-
<input
88+
<input
8989
type="text"
9090
:value="foo"
9191
@input="$emit('update:foo', $event.target.value)">
@@ -97,11 +97,11 @@ app.component('my-component', {
9797
<my-component v-model:foo="bar"></my-component>
9898
```
9999

100-
## Multiple `v-model` bindings
100+
## 複数の `v-model` のバインディング
101101

102-
By leveraging the ability to target a particular prop and event as we learned before with [`v-model` arguments](#v-model-arguments), we can now create multiple v-model bindings on a single component instance.
102+
以前 [`v-model` 引数](#v-model-arguments) で学習した特定のプロパティとイベントをターゲットにする機能を活用することで、単一のコンポーネントインスタンスに対して、複数の v-model バインディングを作成できるようになりました。
103103

104-
Each v-model will sync to a different prop, without the need for extra options in the component:
104+
それぞれの v-model は、コンポーネントに追加オプションを必要とせず、異なるプロパティに同期します。
105105

106106
```html
107107
<user-name
@@ -119,7 +119,7 @@ app.component('user-name', {
119119
lastName: String
120120
},
121121
template: `
122-
<input
122+
<input
123123
type="text"
124124
:value="firstName"
125125
@input="$emit('update:firstName', $event.target.value)">
@@ -139,15 +139,17 @@ app.component('user-name', {
139139
</p>
140140
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
141141

142-
## Handling `v-model` modifiers
142+
## `v-model` 修飾子の処理
143143

144144
When we were learning about form input bindings, we saw that `v-model` has [built-in modifiers](/guide/forms.html#modifiers) - `.trim`, `.number` and `.lazy`. In some cases, however, you might also want to add your own custom modifiers.
145145

146-
Let's create an example custom modifier, `capitalize`, that capitalizes the first letter of the string provided by the `v-model` binding.
146+
フォーム入力バインディングについて学習していたときに、 `v-model`[組み込み修飾子](/guide/forms.html#modifiers) -`.trim``.number` 、および `.lazy` があることがわかりました。ただし、場合によっては、独自のカスタム修飾子を追加することもできます。
147+
148+
`v-model` バインディングによって提供される文字列の最初の文字を大文字にするカスタム修飾子の例、`capitalize`を作成してみましょう。
147149

148-
Modifiers added to a component `v-model` will be provided to the component via the `modelModifiers` prop. In the below example, we have created a component that contains a `modelModifiers` prop that defaults to an empty object.
150+
コンポーネント `v-model` に追加された修飾子は、`modelModifiers` プロパティを介してコンポーネントに提供されます。以下の例では、デフォルトで空のオブジェクトになる `modelModifiers` プロパティを含むコンポーネントを作成しました。
149151

150-
Notice that when the component's `created` lifecycle hook triggers, the `modelModifiers` prop contains `capitalize` and its value is `true` - due to it being set on the `v-model` binding `v-model.capitalize="bar"`.
152+
コンポーネントの `created` ライフサイクルフックがトリガーされると、`modelModifiers` プロパティには `capitalize` が含まれ、その値は`true` になります。これは、 `v-model` バインディングに `v-model.capitalize = "var"` が設定されているためです。
151153

152154
```html
153155
<my-component v-model.capitalize="bar"></my-component>
@@ -162,7 +164,7 @@ app.component('my-component', {
162164
}
163165
},
164166
template: `
165-
<input type="text"
167+
<input type="text"
166168
:value="modelValue"
167169
@input="$emit('update:modelValue', $event.target.value)">
168170
`,
@@ -172,7 +174,7 @@ app.component('my-component', {
172174
})
173175
```
174176

175-
Now that we have our prop set up, we can check the `modelModifiers` object keys and write a handler to change the emitted value. In the code below we will capitalize the string whenever the `<input />` element fires an `input` event.
177+
プロパティを設定したので、 `modelModifiers` オブジェクトのキーを確認し、発行された値を変更するハンドラーを記述できます。以下のコードでは、 `<input />` 要素が `input` イベントを発生させるたびに文字列を大文字にします。
176178

177179
```html
178180
<div id="app">
@@ -215,7 +217,7 @@ app.component('my-component', {
215217
app.mount('#app')
216218
```
217219

218-
For `v-model` bindings with arguments, the generated prop name will be `arg + "Modifiers"`:
220+
引数を持つ `v-model` バインディングの場合、生成されるプロパティ名は `arg + "Modifiers"` になります。
219221

220222
```html
221223
<my-component v-model:foo.capitalize="bar"></my-component>
@@ -225,7 +227,7 @@ For `v-model` bindings with arguments, the generated prop name will be `arg + "M
225227
app.component('my-component', {
226228
props: ['foo', 'fooModifiers'],
227229
template: `
228-
<input type="text"
230+
<input type="text"
229231
:value="foo"
230232
@input="$emit('update:foo', $event.target.value)">
231233
`,

0 commit comments

Comments
 (0)