Skip to content

Migration > Listeners Removed の翻訳を追従 #248

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 3 commits into from
Apr 14, 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 @@ -128,6 +128,7 @@ const sidebar = {
'migration/global-api-treeshaking',
'migration/inline-template-attribute',
'migration/keycode-modifiers',
'migration/listeners-removed',
'migration/props-default-this',
'migration/render-function-api',
'migration/slots-unification',
Expand Down
74 changes: 74 additions & 0 deletions src/guide/migration/listeners-removed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: $listeners の削除
badges:
- breaking
---

# `$listeners` の削除 <MigrationBadges :badges="$frontmatter.badges" />

## 概要

`$listeners` オブジェクトは Vue 3 で削除されました。イベントリスナは `$attrs` の一部になりました。

```javascript
{
text: 'this is an attribute',
onClose: () => console.log('close Event triggered')
}
```

## 2.x での構文

Vue 2 では、コンポーネントに渡された属性は `this.$attrs` で、イベントリスナは `this.$listeners` でアクセスできます。
`inheritAttrs: false` と組み合わせることで、開発者はこれらの属性やリスナを、ルート要素ではなく他の要素に適用することができます:

```html
<template>
<label>
<input type="text" v-bind="$attrs" v-on="$listeners" />
</label>
</template>
<script>
export default {
inheritAttrs: false
}
</script>
```

## 3.x での構文

Vue 3 の仮想 DOM では、イベントリスナはプレフィックスに `on` がついた単なる属性になり、 `$attrs` オブジェクトの一部であるため、 `$listeners` は削除されました。

```vue
<template>
<label>
<input type="text" v-bind="$attrs" />
</label>
</template>
<script>
export default {
inheritAttrs: false
}
</script>
```

もしこのコンポーネントが `id` 属性と `v-on:close` リスナを受け取った場合、 `$attrs` オブジェクトは次のようになります:

```javascript
{
id: 'my-input',
onClose: () => console.log('close Event triggered')
}
```

## 移行の戦略

`$listeners` の使用をすべて削除します。

## 参照

- [関連する RFC](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0031-attr-fallthrough.md)
- [移行ガイド - `class` と `style` を含む `$attrs`](./attrs-includes-class-style.md)
- [移行ガイド - Render 関数 API](./render-function-api.md)
- [移行ガイド - 新しい Emits のオプション](./emits-option.md)
- [移行ガイド - `.native` 修飾子の削除](./v-on-native-modifier-removed.md)