Skip to content

Commit cb87291

Browse files
authored
docs: Essentials > Event Handling の翻訳 (#87)
* translate event handling * 改行の修正 * 改行の修正
1 parent 26587b0 commit cb87291

File tree

1 file changed

+68
-68
lines changed

1 file changed

+68
-68
lines changed

src/guide/events.md

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Event Handling
1+
# イベントハンドリング
22

3-
<div class="vueschool"><a href="https://vueschool.io/lessons/vuejs-user-events?friend=vuejs" target="_blank" rel="sponsored noopener" title="Learn how to handle events on Vue School">Learn how to handle events in a free Vue School lesson</a></div>
3+
<div class="vueschool"><a href="https://vueschool.io/lessons/vuejs-user-events?friend=vuejs" target="_blank" rel="sponsored noopener" title="Learn how to handle events on Vue School">イベントハンドリングする方法を Vue School の無料レッスンで学ぶ</a></div>
44

5-
## Listening to Events
5+
## イベントの購読
66

7-
We can use the `v-on` directive, which we typically shorten to the `@` symbol, to listen to DOM events and run some JavaScript when they're triggered. The usage would be `v-on:click="methodName"` or with the shortcut, `@click="methodName"`
7+
`v-on` ディレクティブを使うことで、DOM イベントの購読、イベント発火時の JavaScript の実行が可能になります。これは通常 `@` に省略することができます。`v-on:click="methodName"` もしくは `@click="methodName"` と書いて使用します。
88

9-
For example:
9+
:
1010

1111
```html
1212
<div id="basic-event">
@@ -25,7 +25,7 @@ Vue.createApp({
2525
}).mount('#basic-event')
2626
```
2727

28-
Result:
28+
結果:
2929

3030
<p class="codepen" data-height="300" data-theme-id="39028" data-default-tab="html,result" data-user="Vue" data-slug-hash="xxGadPZ" data-editable="true" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Event handling: basic">
3131
<span>See the Pen <a href="https://codepen.io/team/Vue/pen/xxGadPZ">
@@ -34,15 +34,15 @@ Result:
3434
</p>
3535
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
3636

37-
## Method Event Handlers
37+
## メソッドイベントハンドラ
3838

39-
The logic for many event handlers will be more complex though, so keeping your JavaScript in the value of the `v-on` attribute isn't feasible. That's why `v-on` can also accept the name of a method you'd like to call.
39+
多くのイベントハンドラのロジックはより複雑になっていくので、`v-on` 属性の値に JavaScript 式を記述し続けるのは現実的ではありません。そのため、`v-on` は呼び出したいメソッドの名前も受け付けます。
4040

41-
For example:
41+
:
4242

4343
```html
4444
<div id="event-with-method">
45-
<!-- `greet` is the name of a method defined below -->
45+
<!-- `greet` は、あらかじめ定義したメソッドの名前 -->
4646
<button @click="greet">Greet</button>
4747
</div>
4848
```
@@ -56,9 +56,9 @@ Vue.createApp({
5656
},
5757
methods: {
5858
greet(event) {
59-
// `this` inside methods points to the current active instance
59+
// メソッド内の `this` は、 Vue インスタンスを参照します
6060
alert('Hello ' + this.name + '!')
61-
// `event` is the native DOM event
61+
// `event` は、ネイティブ DOM イベントです
6262
if (event) {
6363
alert(event.target.tagName)
6464
}
@@ -67,7 +67,7 @@ Vue.createApp({
6767
}).mount('#event-with-method')
6868
```
6969

70-
Result:
70+
結果:
7171

7272
<p class="codepen" data-height="300" data-theme-id="39028" data-default-tab="js,result" data-user="Vue" data-slug-hash="jOPvmaX" data-editable="true" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Event handling: with a method">
7373
<span>See the Pen <a href="https://codepen.io/team/Vue/pen/jOPvmaX">
@@ -76,9 +76,9 @@ Result:
7676
</p>
7777
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
7878

79-
## Methods in Inline Handlers
79+
## インラインメソッドハンドラ
8080

81-
Instead of binding directly to a method name, we can also use methods in an inline JavaScript statement:
81+
メソッド名を直接指定する代わりに、インライン JavaScript 式でメソッドを指定することもできます:
8282

8383
```html
8484
<div id="inline-handler">
@@ -97,7 +97,7 @@ Vue.createApp({
9797
}).mount('#inline-handler')
9898
```
9999

100-
Result:
100+
結果:
101101

102102
<p class="codepen" data-height="300" data-theme-id="39028" data-default-tab="html,result" data-user="Vue" data-slug-hash="WNvgjda" data-editable="true" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Event handling: with an inline handler">
103103
<span>See the Pen <a href="https://codepen.io/team/Vue/pen/WNvgjda">
@@ -106,7 +106,7 @@ Result:
106106
</p>
107107
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
108108

109-
Sometimes we also need to access the original DOM event in an inline statement handler. You can pass it into a method using the special `$event` variable:
109+
時には、インラインステートメントハンドラでオリジナルの DOM イベントを参照したいこともあるでしょう。特別な `$event` 変数を使うことでメソッドに DOM イベントを渡すことができます:
110110

111111
```html
112112
<button @click="warn('Form cannot be submitted yet.', $event)">
@@ -118,7 +118,7 @@ Sometimes we also need to access the original DOM event in an inline statement h
118118
// ...
119119
methods: {
120120
warn(message, event) {
121-
// now we have access to the native event
121+
// ネイティブイベントを参照しています
122122
if (event) {
123123
event.preventDefault()
124124
}
@@ -127,12 +127,12 @@ methods: {
127127
}
128128
```
129129

130-
## Multiple Event Handlers
130+
## 複数イベントハンドラ
131131

132-
You can have multiple methods in an event handler separated by a comma operator like this:
132+
イベントハンドラ内ではカンマで区切ることで、複数のメソッドを設定することができます:
133133

134134
```html
135-
<!-- both one() and two() will execute on button click -->
135+
<!-- ボタンをクリックすると、one()とtwo()の両方が実行されます -->
136136
<button @click="one($event), two($event)">
137137
Submit
138138
</button>
@@ -142,19 +142,19 @@ You can have multiple methods in an event handler separated by a comma operator
142142
// ...
143143
methods: {
144144
one(event) {
145-
// first handler logic...
145+
// one($event)のハンドラーロジック
146146
},
147147
two(event) {
148-
// second handler logic...
148+
// two($event)のハンドラーロジック
149149
}
150150
}
151151
```
152152

153-
## Event Modifiers
153+
## イベント修飾子
154154

155-
It is a very common need to call `event.preventDefault()` or `event.stopPropagation()` inside event handlers. Although we can do this easily inside methods, it would be better if the methods can be purely about data logic rather than having to deal with DOM event details.
155+
イベントハンドラ内での `event.preventDefault()` または `event.stopPropagation()` の呼び出しは、様々な場面で共通に必要になります。これらはメソッド内部で簡単に呼び出すことができますが、DOM イベントの込み入った処理をおこなうよりも、純粋なデータロジックだけになっている方がより良いでしょう。
156156

157-
To address this problem, Vue provides **event modifiers** for `v-on`. Recall that modifiers are directive postfixes denoted by a dot.
157+
この問題に対応するために、Vue `v-on` のために **イベント修飾子(event modifiers)** を提供しています。修飾子は、ドット(.)で表記されるディレクティブの接尾辞を思い返してください。
158158

159159
- `.stop`
160160
- `.prevent`
@@ -164,98 +164,98 @@ To address this problem, Vue provides **event modifiers** for `v-on`. Recall tha
164164
- `.passive`
165165

166166
```html
167-
<!-- the click event's propagation will be stopped -->
167+
<!-- クリックイベントの伝搬が止まります -->
168168
<a @click.stop="doThis"></a>
169169

170-
<!-- the submit event will no longer reload the page -->
170+
<!-- submit イベントによってページがリロードされません -->
171171
<form @submit.prevent="onSubmit"></form>
172172

173-
<!-- modifiers can be chained -->
173+
<!-- 修飾子は繋げることができます -->
174174
<a @click.stop.prevent="doThat"></a>
175175

176-
<!-- just the modifier -->
176+
<!-- 値を指定せず、修飾子だけ利用することもできます -->
177177
<form @submit.prevent></form>
178178

179-
<!-- use capture mode when adding the event listener -->
180-
<!-- i.e. an event targeting an inner element is handled here before being handled by that element -->
179+
<!-- イベントリスナーを追加するときにキャプチャモードで使います -->
180+
<!-- 言い換えれば、内部要素を対象とするイベントは、その要素によって処理される前にここで処理されます -->
181181
<div @click.capture="doThis">...</div>
182182

183-
<!-- only trigger handler if event.target is the element itself -->
184-
<!-- i.e. not from a child element -->
183+
<!-- event.target が要素自身のときだけ、ハンドラが呼び出されます -->
184+
<!-- 言い換えると子要素のときは呼び出されません -->
185185
<div @click.self="doThat">...</div>
186186
```
187187

188188
::: tip
189-
Order matters when using modifiers because the relevant code is generated in the same order. Therefore using `@click.prevent.self` will prevent **all clicks** while `@click.self.prevent` will only prevent clicks on the element itself.
189+
修飾子を使用するとき、関連するコードが同じ順序で生成されるため注意してください。それゆえ、`@click.prevent.self` を使用すると**全てのクリックイベント**を防ぐことはできますが、`@click.self.prevent` は要素自身におけるクリックイベントを防ぐだけです。
190190
:::
191191

192192
```html
193-
<!-- the click event will be triggered at most once -->
193+
<!-- 最大1回、クリックイベントはトリガされます -->
194194
<a @click.once="doThis"></a>
195195
```
196196

197-
Unlike the other modifiers, which are exclusive to native DOM events, the `.once` modifier can also be used on [component events](component-custom-events.html). If you haven't read about components yet, don't worry about this for now.
197+
他の修飾子とは違って、ネイティブ DOM イベント専用ではありますが、`.once` 修飾子を[コンポーネントイベント](component-custom-events.html)でも使用することができます。まだコンポーネントについて読んでいないなら、今は気にする必要はありません。
198198

199-
Vue also offers the `.passive` modifier, corresponding to [`addEventListener`'s `passive` option](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters).
199+
Vue [`addEventListener` `passive` オプション](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters)に対応する `.passive` 修飾子も提供しています。
200200

201201
```html
202-
<!-- the scroll event's default behavior (scrolling) will happen -->
203-
<!-- immediately, instead of waiting for `onScroll` to complete -->
204-
<!-- in case it contains `event.preventDefault()` -->
202+
<!-- `onScroll` が `event.preventDefault()` を含んでいたとしても -->
203+
<!-- スクロールイベントのデフォルトの挙動(つまりスクロール)は -->
204+
<!-- イベントの完了を待つことなくただちに発生するようになります -->
205205
<div @scroll.passive="onScroll">...</div>
206206
```
207207

208-
The `.passive` modifier is especially useful for improving performance on mobile devices.
208+
`.passive` 修飾子は特にモバイルでのパフォーマンスを改善するのに有用です。
209209

210210
::: tip
211-
Don't use `.passive` and `.prevent` together, because `.prevent` will be ignored and your browser will probably show you a warning. Remember, `.passive` communicates to the browser that you _don't_ want to prevent the event's default behavior.
211+
`.passive` `.prevent` を一緒に使わないでください。`.prevent `は無視され、ブラウザにはおそらく警告が表示されます。`.passive` はイベントのデフォルトの挙動を妨げないことをブラウザに伝達することを思い出してください。
212212
:::
213213

214-
## Key Modifiers
214+
## キー修飾子
215215

216-
When listening for keyboard events, we often need to check for specific keys. Vue allows adding key modifiers for `v-on` or `@` when listening for key events:
216+
キーボードイベントを購読するにあたって、特定のキーのチェックが必要になることがあります。Vue では、`v-on` または `@` に対してキー修飾子を追加することができます:
217217

218218
```html
219-
<!-- only call `vm.submit()` when the `key` is `Enter` -->
219+
<!-- `key` が `Enter` のときだけ、`vm.submit()` が呼ばれます -->
220220
<input @keyup.enter="submit" />
221221
```
222222

223-
You can directly use any valid key names exposed via [`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) as modifiers by converting them to kebab-case.
223+
[`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) で公開されている任意のキー名は、ケバブケースに変換することで修飾子として直接使用できます。
224224

225225
```html
226226
<input @keyup.page-down="onPageDown" />
227227
```
228228

229-
In the above example, the handler will only be called if `$event.key` is equal to `'PageDown'`.
229+
上の例では、ハンドラは `$event.key ``'PageDown'` に等しい場合だけ呼ばれます。
230230

231-
### Key Aliases
231+
### キーコード
232232

233-
Vue provides aliases for the most commonly used keys:
233+
Vue は最も一般的に使用されるキーコードのエイリアスを提供しています:
234234

235235
- `.enter`
236236
- `.tab`
237-
- `.delete` (captures both "Delete" and "Backspace" keys)
237+
- `.delete` (Delete” と “Backspace” キー両方をキャプチャします)
238238
- `.esc`
239239
- `.space`
240240
- `.up`
241241
- `.down`
242242
- `.left`
243243
- `.right`
244244

245-
## System Modifier Keys
245+
## システム修飾子キー
246246

247-
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressed:
247+
次の修飾子を使用すると、対応するキーが押されたときにのみマウスもしくはキーボードのイベントリスナをトリガできます:
248248

249249
- `.ctrl`
250250
- `.alt`
251251
- `.shift`
252252
- `.meta`
253253

254254
::: tip Note
255-
On Macintosh keyboards, meta is the command key (⌘). On Windows keyboards, meta is the Windows key (⊞). On Sun Microsystems keyboards, meta is marked as a solid diamond (◆). On certain keyboards, specifically MIT and Lisp machine keyboards and successors, such as the Knight keyboard, space-cadet keyboard, meta is labeled “META”. On Symbolics keyboards, meta is labeled “META” or “Meta”.
255+
注意: Macintosh キーボードの場合、meta はコマンドキー(⌘)です。Windows のキーボードでは、meta はウィンドウキー(⊞)です。Sun Microsystems のキーボードでは、メタは実線のダイヤモンド(◆)とマークされています。特定のキーボードでは、特に MIT Lisp マシンのキーボードと Knight キーボード、space-cadet キーボード、メタのような後継機には “META” と表示されます。 Symbolics のキーボードでは、 “META” または “Meta” というラベルが付いています。
256256
:::
257257

258-
For example:
258+
:
259259

260260
```html
261261
<!-- Alt + Enter -->
@@ -266,38 +266,38 @@ For example:
266266
```
267267

268268
::: tip
269-
Note that modifier keys are different from regular keys and when used with `keyup` events, they have to be pressed when the event is emitted. In other words, `keyup.ctrl` will only trigger if you release a key while holding down `ctrl`. It won't trigger if you release the `ctrl` key alone
269+
修飾子キーは通常のキーとは異なり、`keyup` イベントで使用する場合、イベント発生時に押されていなければならないことに注意してください。言い換えると、`keyup.ctrl` `ctrl` を押しながら何かのキーを離したときにのみ、トリガされます。`ctrl` キーだけを離しても、トリガされません。
270270
:::
271271

272-
### `.exact` Modifier
272+
### `.exact` 修飾子
273273

274-
The `.exact` modifier allows control of the exact combination of system modifiers needed to trigger an event.
274+
`.exact` 修飾子はイベントを引き起こすために必要なシステム修飾子の正確な組み合わせを制御します。
275275

276276
```html
277-
<!-- this will fire even if Alt or Shift is also pressed -->
277+
<!-- これは Ctrl に加えて Alt Shift キーが押されていても発行されます -->
278278
<button @click.ctrl="onClick">A</button>
279279

280-
<!-- this will only fire when Ctrl and no other keys are pressed -->
280+
<!-- これは Ctrl キーが押され、他のキーが押されてないときだけ発行されます -->
281281
<button @click.ctrl.exact="onCtrlClick">A</button>
282282

283-
<!-- this will only fire when no system modifiers are pressed -->
283+
<!-- これは システム修飾子が押されてないときだけ発行されます -->
284284
<button @click.exact="onClick">A</button>
285285
```
286286

287-
### Mouse Button Modifiers
287+
### マウスボタンの修飾子
288288

289289
- `.left`
290290
- `.right`
291291
- `.middle`
292292

293-
These modifiers restrict the handler to events triggered by a specific mouse button.
293+
これらの修飾子は、イベントのトリガのハンドリングを、特定のマウスのボタンのみに制限します。
294294

295-
## Why Listeners in HTML?
295+
## なぜ HTML にリスナを記述するのですか
296296

297-
You might be concerned that this whole event listening approach violates the good old rules about "separation of concerns". Rest assured - since all Vue handler functions and expressions are strictly bound to the ViewModel that's handling the current view, it won't cause any maintenance difficulty. In fact, there are several benefits in using `v-on` or `@`:
297+
これまで説明してきたようなイベント監視のアプローチは、”関心の分離”という古き良きルールを破っているのではないか、と心配されるかもしれません。安心してください。すべての Vue ハンドラ関数と式は、現在の View を扱う ViewModel に厳密に閉じています。それによってメンテナンスが難しくなることはありません。実際、`v-on` または `@` を利用することでいくつかの利点があります。
298298

299-
1. It's easier to locate the handler function implementations within your JS code by skimming the HTML template.
299+
1. HTML テンプレートを眺めることで、JS コード内のハンドラ関数を探すことを容易にします
300300

301-
2. Since you don't have to manually attach event listeners in JS, your ViewModel code can be pure logic and DOM-free. This makes it easier to test.
301+
2. JS 内のイベントリスナーを手作業でアタッチする必要がないので、ViewModel DOM 依存のない純粋なロジックにできます。これはテスタビリティを向上させます。
302302

303-
3. When a ViewModel is destroyed, all event listeners are automatically removed. You don't need to worry about cleaning it up yourself.
303+
3. ViewModel が消去されるときに、すべてのイベントリスナーは自動で削除されます。手動でそれらの消去をおこなうことを気にする必要はありません。

0 commit comments

Comments
 (0)