Skip to content

Commit 2b8a958

Browse files
authored
Merge pull request #161 from daisuke85a/migration-guide-custom-directives
Migration Guide > Custom Directives の翻訳元ファイル更新と翻訳
2 parents 4bf5e33 + 61bd806 commit 2b8a958

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

src/guide/migration/custom-directives.md

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,30 @@ badges:
33
- breaking
44
---
55

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

8-
## Overview
8+
## 概要
99

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

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

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

17-
## 2.x Syntax
16+
## 2.x の文法
1817

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

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

27-
Here’s an example of this:
26+
この例を次に示します。
2827

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

3332
```js
@@ -38,37 +37,38 @@ Vue.directive('highlight', {
3837
})
3938
```
4039

41-
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.
40+
この要素の初期設定では、ディレクティブは値を渡してスタイルをバインドします。値は、アプリケーションにてさまざまな値に更新できます。
4241

43-
## 3.x Syntax
42+
## 3.x の文法
4443

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

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

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

5757
```js
5858
const MyDirective = {
5959
beforeMount(el, binding, vnode, prevVnode) {},
6060
mounted() {},
6161
beforeUpdate() {},
6262
updated() {},
63-
beforeUnmount() {}, // new
63+
beforeUnmount() {}, // 追加
6464
unmounted() {}
6565
}
6666
```
6767

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

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

7474
```js
@@ -81,28 +81,28 @@ app.directive('highlight', {
8181
})
8282
```
8383

84-
Now that the custom directive lifecycle hooks mirror those of the components themselves, they become easier to reason about and remember!
85-
86-
## Implementation Details
84+
カスタムディレクティブのライフサイクルフックがコンポーネントと同じになったことで、理由を説明したり、覚えるのがより簡単になりました。
8785

88-
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:
86+
### 特別な問題への対処: コンポーネントのインスタンスへのアクセス
8987

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

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

100-
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`.
92+
```javascript
93+
bind(el, binding, vnode) {
94+
const vm = vnode.context
95+
}
96+
```
10197

102-
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:
98+
Vue 3 では、インスタンスは `binding` の一部になりました。
10399

104-
```html
105-
<div @vnodeMounted="myHook" />
100+
```javascript
101+
mounted(el, binding, vnode) {
102+
const vm = binding.instance
103+
}
106104
```
107105

108-
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.
106+
:::warning
107+
[fragments](/guide/migration/fragments.html#overview) のサポートにより、コンポーネントは複数のルートノードを持つ可能性があります。マルチルートコンポーネントに適用すると、ディレクティブは無視され、警告がスローされます。
108+
:::

0 commit comments

Comments
 (0)