Skip to content

Commit 9c4deea

Browse files
committed
docs: add various improvements to the Events API migration guide
vuejs/docs@d9430c2
1 parent b808e8a commit 9c4deea

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

src/guide/migration/events-api.md

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,46 @@ badges:
77

88
## 概要
99

10-
インスタンスメソッド `$on``$off``$once` は削除されました。アプリケーションインスタンスはイベントエミッタインタフェースを実装しなくなりました
10+
インスタンスメソッド `$on``$off``$once` は削除されました。コンポーネントインスタンスはイベントエミッタインタフェースを実装しなくなりました
1111

1212
## 2.x での構文
1313

14-
2.x では、Vue インスタンスを使用して、イベントエミッタ API (`$on``$off``$once`) を介して強制的に接続されたハンドラをトリガすることができました。これは、アプリケーション全体で使用されるグローバルイベントリスナーを作成するための _イベントハブ_ を作るために使用されました
14+
2.x では、Vue インスタンスを使用して、イベントエミッタ API (`$on``$off``$once`) を介して強制的に接続されたハンドラをトリガすることができました。これは、アプリケーション全体で使用されるグローバルイベントリスナーを作成するための _イベントバス_ を作るために使用できました
1515

1616
```js
17-
// eventHub.js
17+
// eventBus.js
1818

19-
const eventHub = new Vue()
19+
const eventBus = new Vue()
2020

21-
export default eventHub
21+
export default eventBus
2222
```
2323

2424
```js
2525
// ChildComponent.vue
26-
import eventHub from './eventHub'
26+
import eventBus from './eventBus'
2727

2828
export default {
2929
mounted() {
30-
// eventHub リスナーの追加
31-
eventHub.$on('custom-event', () => {
30+
// eventBus リスナーの追加
31+
eventBus.$on('custom-event', () => {
3232
console.log('Custom event triggered!')
3333
})
3434
},
3535
beforeDestroy() {
36-
// eventHub リスナーの削除
37-
eventHub.$off('custom-event')
36+
// eventBus リスナーの削除
37+
eventBus.$off('custom-event')
3838
},
3939
}
4040
```
4141

4242
```js
4343
// ParentComponent.vue
44-
import eventHub from './eventHub'
44+
import eventBus from './eventBus'
4545

4646
export default {
4747
methods: {
4848
callGlobalCustomEvent() {
49-
eventHub.$emit('custom-event') // ChildComponent がマウントされている場合、コンソールにメッセージが表示されます。
49+
eventBus.$emit('custom-event') // ChildComponent がマウントされている場合、コンソールにメッセージが表示されます。
5050
},
5151
},
5252
}
@@ -58,26 +58,47 @@ export default {
5858

5959
## 移行の戦略
6060

61+
[Migration build flag: `INSTANCE_EVENT_EMITTER`](migration-build.html#compat-configuration)
62+
6163
Vue 3 では、これらの API を使用して、コンポーネント内からコンポーネント自身が発行したイベントを購読することはできなくなりました。そのユースケースのための移行パスはありません。
6264

63-
ただし、EventHub (イベントハブ) パターンは、Event Emitter (イベントエミッタ) インタフェースを実装した外部ライブラリを使用することで置き換えることができます。例えば、[mitt](https://github.com/developit/mitt)[tiny-emitter](https://github.com/scottcorgan/tiny-emitter) などです。
65+
### Root Component Events
66+
67+
Static event listeners can be added to the root component by passing them as props to `createApp`:
68+
69+
```js
70+
createApp(App, {
71+
// Listen for the 'expand' event
72+
onExpand() {
73+
console.log('expand')
74+
}
75+
})
76+
```
77+
78+
### Event Bus
79+
80+
EventBus (イベントバス) パターンは、Event Emitter (イベントエミッタ) インタフェースを実装した外部ライブラリを使用することで置き換えることができます。例えば、[mitt](https://github.com/developit/mitt)[tiny-emitter](https://github.com/scottcorgan/tiny-emitter) などです。
6481

6582
例:
6683

6784
```js
68-
// eventHub.js
85+
// eventBus.js
6986
import emitter from 'tiny-emitter/instance'
7087

7188
export default {
7289
$on: (...args) => emitter.on(...args),
7390
$once: (...args) => emitter.once(...args),
7491
$off: (...args) => emitter.off(...args),
75-
$emit: (...args) => emitter.emit(...args),
92+
$emit: (...args) => emitter.emit(...args)
7693
}
7794
```
7895

7996
これは Vue 2 と同じような Event Emitter API を提供します。
8097

81-
これらのメソッドは、Vue 3 の将来の互換ビルドでもサポートされる可能性があります。
98+
In most circumstances, using a global event bus for communicating between components is discouraged. While it is often the simplest solution in the short term, it almost invariably proves to be a maintenance headache in the long term. Depending on the circumstances, there are various alternatives to using an event bus:
8299

83-
[移行ビルドのフラグ: `INSTANCE_EVENT_EMITTER`](migration-build.html#compat-の設定)
100+
* [Props](/guide/component-basics.html#passing-data-to-child-components-with-props) and [events](/guide/component-basics.html#listening-to-child-components-events) should be your first choice for parent-child communication. Siblings can communicate via their parent.
101+
* [Provide and inject](/guide/component-provide-inject.html) allow a component to communicate with its slot contents. This is useful for tightly-coupled components that are always used together.
102+
* `provide`/`inject` can also be used for long-distance communication between components. It can help to avoid 'prop drilling', where props need to be passed down through many levels of components that don't need those props themselves.
103+
* Prop drilling can also be avoided by refactoring to use slots. If an interim component doesn't need the props then it might indicate a problem with separation of concerns. Introducing a slot in that component allows the parent to create the content directly, so that props can be passed without the interim component needing to get involved.
104+
* [Global state management](/guide/state-management.html), such as [Vuex](https://next.vuex.vuejs.org/).

0 commit comments

Comments
 (0)