Skip to content

Migration Guide > Custom Directives の翻訳元ファイル更新と翻訳 #161

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
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
84 changes: 42 additions & 42 deletions src/guide/migration/custom-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@ badges:
- breaking
---

# Custom Directives <MigrationBadges :badges="$frontmatter.badges" />
# カスタムディレクティブ <MigrationBadges :badges="$frontmatter.badges" />

## Overview
## 概要

Here is a quick summary of what has changed:
変更点の概要は次のとおりです。

- API has been renamed to better align with component lifecycle
- Custom directives will be controlled by child component via `v-bind="$attrs"`
- API は、コンポーネントのライフサイクルとの整合性を高めるために名前が変更されました

For more information, read on!
詳細については、以下をお読みください。

## 2.x Syntax
## 2.x の文法

In Vue 2, custom directives were created by using the hooks listed below to target an element’s lifecycle, all of which are optional:
Vue 2 では、以下のフックを使用して要素のライフサイクルをターゲットにしたカスタムディレクティブが作成していました。これらはすべてオプションです。

- **bind** - Occurs once the directive is bound to the element. Occurs only once.
- **inserted** - Occurs once the element is inserted into the parent DOM.
- **update** - This hook is called when the element updates, but children haven't been updated yet.
- **componentUpdated** - This hook is called once the component and the children have been updated.
- **unbind** - This hook is called once the directive is removed. Also called only once.
- **bind** - ディレクティブが要素にバインドされると発生します。これは一度だけ発生します。
- **inserted** - 要素が親 DOM に挿入された後に発生します。
- **update** - 要素が更新されたときに呼び出されますが、子はまだ更新されていません。
- **componentUpdated** - コンポーネントと子が更新されると呼び出されます。
- **unbind** - ディレクティブが削除されると呼び出されます。 また、これは一度だけ呼び出されます。

Here’s an example of this:
この例を次に示します。

```html
<p v-highlight="yellow">Highlight this text bright yellow</p>
<p v-highlight="'yellow'">このテキストを明るい黄色で強調表示します</p>
```

```js
Expand All @@ -38,37 +37,38 @@ Vue.directive('highlight', {
})
```

Here, in the initial setup for this element, the directive binds a style by passing in a value, that can be updated to different values through the application.
この要素の初期設定では、ディレクティブは値を渡してスタイルをバインドします。値は、アプリケーションにてさまざまな値に更新できます。

## 3.x Syntax
## 3.x の文法

In Vue 3, however, we’ve created a more cohesive API for custom directives. As you can see, they differ greatly from our component lifecycle methods even though we’re hooking into similar events. We’ve now unified them like so:

ただし、Vue 3 では、カスタムディレクティブ用のよりまとまりのある API を作成しました。Vue 2 では、似たようなイベントにフックしているにもかかわらず、コンポーネントのライフサイクルメソッドとは大きく異なります。これらを次のように統合しました。

- bind → **beforeMount**
- inserted → **mounted**
- **beforeUpdate**: new! this is called before the element itself is updated, much like the component lifecycle hooks.
- update → removed! There were too many similarities to updated, so this is redundant. Please use updated instead.
- **beforeUpdate**: 追加されました! これは、コンポーネントのライフサイクルフックのように、要素自体が更新される前に呼び出されます。
- update → 削除されました! updated と似たようなものが多すぎて冗長です。代わりに updated を使ってください。
- componentUpdated → **updated**
- **beforeUnmount** new! similar to component lifecycle hooks, this will be called right before an element is unmounted.
- **beforeUnmount**: 追加されました! コンポーネントのライフサイクルフックと同様に、要素がマウント解除される直前に呼び出されます。
- unbind -> **unmounted**

The final API is as follows:
最終的な API は次のとおりです。

```js
const MyDirective = {
beforeMount(el, binding, vnode, prevVnode) {},
mounted() {},
beforeUpdate() {},
updated() {},
beforeUnmount() {}, // new
beforeUnmount() {}, // 追加
unmounted() {}
}
```

The resulting API could be used like this, mirroring the example from earlier:
Vue 3 の API は、先ほどの例を用いてこのように使用できます。

```html
<p v-highlight="yellow">Highlight this text bright yellow</p>
<p v-highlight="'yellow'">このテキストを明るい黄色で強調表示します</p>
```

```js
Expand All @@ -81,28 +81,28 @@ app.directive('highlight', {
})
```

Now that the custom directive lifecycle hooks mirror those of the components themselves, they become easier to reason about and remember!

## Implementation Details
カスタムディレクティブのライフサイクルフックがコンポーネントと同じになったことで、理由を説明したり、覚えるのがより簡単になりました。

In Vue 3, we're now supporting fragments, which allow us to return more than one DOM node per component. You can imagine how handy that is for something like a component with multiple lis or the children elements of a table:
### 特別な問題への対処: コンポーネントのインスタンスへのアクセス

```html
<template>
<li>Hello</li>
<li>Vue</li>
<li>Devs!</li>
</template>
```
一般的には、ディレクティブをコンポーネントのインスタンスから独立させることが推奨されています。カスタムディレクティブ内からインスタンスにアクセスする状況は、本来はディレクティブがコンポーネント自体であるべきことが多いです。しかし、まれにこれが有効なこともあります。

As wonderfully flexible as this is, we can potentially encounter a problem with a custom directive that could have multiple root nodes.
Vue 2 では、コンポーネントインスタンスは `vnode` 引数を使ってアクセスする必要がありました。

As a result, custom directives are now included as part of a virtual DOM node’s data. When a custom directive is used on a component, hooks are passed down to the component as extraneous props and end up in `this.$attrs`.
```javascript
bind(el, binding, vnode) {
const vm = vnode.context
}
```

This also means it's possible to directly hook into an element's lifecycle like this in the template, which can be handy when a custom directive is too involved:
Vue 3 では、インスタンスは `binding` の一部になりました。

```html
<div @vnodeMounted="myHook" />
```javascript
mounted(el, binding, vnode) {
const vm = binding.instance
}
```

This is consistent with the attribute fallthrough behavior, so when a child component uses `v-bind="$attrs"` on an inner element, it will apply any custom directives used on it as well.
:::warning
[fragments](/guide/migration/fragments.html#overview) のサポートにより、コンポーネントは複数のルートノードを持つ可能性があります。マルチルートコンポーネントに適用すると、ディレクティブは無視され、警告がスローされます。
:::