Skip to content

Migration > Emits Option の翻訳を追従 #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const sidebar = {
'migration/custom-directives',
'migration/custom-elements-interop',
'migration/data-option',
'migration/emits-option',
'migration/events-api',
'migration/filters',
'migration/fragments',
Expand Down
97 changes: 97 additions & 0 deletions src/guide/migration/emits-option.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
title: emits オプション
badges:
- new
---

# `emits` オプション <MigrationBadges :badges="$frontmatter.badges" />

## 概要

Vue 3 では `emits` オプションが、既存の `props` オプションと同様に提供されるようになりました。このオプションを使用して、コンポーネントが親に発行できるイベントを定義することができます。

## 2.x の挙動

Vue 2 では、コンポーネントが受け取るプロパティを定義することはできますが、コンポーネントが発行できるイベントを宣言することはできません。

```vue
<template>
<div>
<p>{{ text }}</p>
<button v-on:click="$emit('accepted')">OK</button>
</div>
</template>
<script>
export default {
props: ['text']
}
</script>
```

## 3.x の挙動

プロパティと同様に、コンポーネントが発行するイベントは `emits` オプションで定義できるようになりました。

```vue
<template>
<div>
<p>{{ text }}</p>
<button v-on:click="$emit('accepted')">OK</button>
</div>
</template>
<script>
export default {
props: ['text'],
emits: ['accepted']
}
</script>
```

このオプションはオブジェクトも受け取ります。これにより開発者は、発行されたイベントと一緒に渡される引数のバリデータを `props` 定義のバリデータと同様に定義できます。

詳細については、[この機能の API ドキュメント](../../api/options-data.md#emits)をお読みください。

## 移行方針

各コンポーネントから発行されたすべてのイベントを、 `emits` を使って発行することを強くお勧めします。

これは [`.native` 修飾子の削除](./v-on-native-modifier-removed.md) のために特に重要です。 `emits` で宣言されていないイベントのリスナーは、コンポーネントの `$attrs` に含まれるようになり、デフォルトではコンポーネントのルートノードに束縛されます。

### 例

ネイティブイベントを親に再発行するコンポーネントでは、これにより2つのイベントが発火することになります。

```vue
<template>
<button v-on:click="$emit('click', $event)">OK</button>
</template>
<script>
export default {
emits: [] // 宣言されていないイベント
}
</script>
```

親がコンポーネントの `click` イベントをリッスンする場合:

```html
<my-button v-on:click="handleClick"></my-button>
```

これは _2回_ 引き起こされます。

- `$emit()` から一度。
- ルート要素に適用されたネイティブイベントリスナから一度。

ここでは2つの選択肢があります。

1. `click` イベントを適切に宣言する。これは `<my-button>` のイベントハンドラに何らかのロジックを実際に追加する場合で役立ちます。
2. `.native` を追加しなくても、親は簡単にネイティブイベントをリッスンできるので、イベントの再発行を削除します。とにかく明らかにイベントを再発行するだけの場合に適しています。

## 関連情報

- [関連する RFC](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md)
- [移行ガイド - `.native` 修飾子の削除](./v-on-native-modifier-removed.md)
- [移行ガイド - `$listeners` の削除](./listeners-removed.md)
- [移行ガイド - `class` と `style` を含む `$attrs`](./attrs-includes-class-style.md)
- [移行ガイド - Render 関数 API の変更](./render-function-api.md)