From d40e4385bebf3586d4c6a065064908c5f71c2ee6 Mon Sep 17 00:00:00 2001 From: Naoki Endoh Date: Fri, 9 Apr 2021 20:52:47 +0900 Subject: [PATCH 1/4] feat: add migration guide > emits option --- src/.vuepress/config.js | 1 + src/guide/migration/emits-option.md | 97 +++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/guide/migration/emits-option.md diff --git a/src/.vuepress/config.js b/src/.vuepress/config.js index 1d29ce3b..e5b88e40 100644 --- a/src/.vuepress/config.js +++ b/src/.vuepress/config.js @@ -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', diff --git a/src/guide/migration/emits-option.md b/src/guide/migration/emits-option.md new file mode 100644 index 00000000..35b40aea --- /dev/null +++ b/src/guide/migration/emits-option.md @@ -0,0 +1,97 @@ +--- +title: emits Option +badges: + - new +--- + +# `emits` Option + +## Overview + +Vue 3 now offers an `emits` option, similar to the existing `props` option. This option can be used to define the events that a component can emit to its parent. + +## 2.x Behavior + +In Vue 2, you can define the props that a component receives, but you can't declare which events it can emit: + +```vue + + +``` + +## 3.x Behavior + +Similar to props, the events that the component emits can now be defined with the `emits` option: + +```vue + + +``` + +The option also accepts an object, which allows the developer to define validators for the arguments that are passed with the emitted event, similar to validators in `props` definitions. + +For more information on this, please read the [API documentation for this feature](../../api/options-data.md#emits). + +## Migration Strategy + +It is highly recommended that you document all of the events emitted by each of your components using `emits`. + +This is especially important because of [the removal of the `.native` modifier](./v-on-native-modifier-removed.md). Any listeners for events that aren't declared with `emits` will now be included in the component's `$attrs`, which by default will be bound to the component's root node. + +### Example + +For components that re-emit native events to their parent, this would now lead to two events being fired: + +```vue + + +``` + +When a parent listens for the `click` event on the component: + +```html + +``` + +it would now be triggered _twice_: + +- Once from `$emit()`. +- Once from a native event listener applied to the root element. + +Here you have two options: + +1. Properly declare the `click` event. This is useful if you actually do add some logic to that event handler in ``. +2. Remove the re-emitting of the event, since the parent can now listen for the native event easily, without adding `.native`. Suitable when you really only re-emit the event anyway. + +## See also + +- [Relevant RFC](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md) +- [Migration guide - `.native` modifier removed](./v-on-native-modifier-removed.md) +- [Migration guide - `$listeners` removed](./listeners-removed.md) +- [Migration guide - `$attrs` includes `class` & `style`](./attrs-includes-class-style.md) +- [Migration guide - Changes in the Render Functions API](./render-function-api.md) From f3e1940e5e4022ca009f1c8412a2d02406799a25 Mon Sep 17 00:00:00 2001 From: Naoki Endoh Date: Fri, 9 Apr 2021 22:20:49 +0900 Subject: [PATCH 2/4] docs: translate migration guide > emits option --- src/guide/migration/emits-option.md | 58 ++++++++++++++--------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/guide/migration/emits-option.md b/src/guide/migration/emits-option.md index 35b40aea..53d0ecd1 100644 --- a/src/guide/migration/emits-option.md +++ b/src/guide/migration/emits-option.md @@ -1,18 +1,18 @@ --- -title: emits Option +title: emits オプション badges: - new --- -# `emits` Option +# `emits` オプション -## Overview +## 概要 -Vue 3 now offers an `emits` option, similar to the existing `props` option. This option can be used to define the events that a component can emit to its parent. +Vue 3 では `emits` オプションが、既存の `props` オプションと同様に提供されるようになりました。このオプションを使用して、コンポーネントが親に発行できるイベントを定義することができます。 -## 2.x Behavior +## 2.x の挙動 -In Vue 2, you can define the props that a component receives, but you can't declare which events it can emit: +Vue 2 では、コンポーネントが受け取るプロパティを定義することはできますが、コンポーネントが発行できるイベントを宣言することはできません。 ```vue