Skip to content

Commit fc9424f

Browse files
committed
Migration Guide > Custom Directives の翻訳 vuejs-jp#110
1 parent 62c26d0 commit fc9424f

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

src/guide/migration/custom-directives.md

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +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
12+
- API は、コンポーネントのライフサイクルとの整合性を高めるために名前が変更されました
1313

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

16-
## 2.x Syntax
16+
## 2.x の文法
1717

18-
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 では、以下のフックを使用して要素のライフサイクルをターゲットにしたカスタムディレクティブが作成していました。これらはすべてオプションです。
1919

20-
- **bind** - Occurs once the directive is bound to the element. Occurs only once.
21-
- **inserted** - Occurs once the element is inserted into the parent DOM.
22-
- **update** - This hook is called when the element updates, but children haven't been updated yet.
23-
- **componentUpdated** - This hook is called once the component and the children have been updated.
24-
- **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** - ディレクティブが削除されると呼び出されます。 1回だけ呼び出されます。
2525

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

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

3232
```js
@@ -37,37 +37,38 @@ Vue.directive('highlight', {
3737
})
3838
```
3939

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

42-
## 3.x Syntax
42+
## 3.x の文法
4343

44-
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 では、似たようなイベントにフックしているにもかかわらず、コンポーネントのライフサイクルメソッドとは大きく異なります。これらを次のように統合しました。
4546

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

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

5657
```js
5758
const MyDirective = {
5859
beforeMount(el, binding, vnode, prevVnode) {},
5960
mounted() {},
6061
beforeUpdate() {},
6162
updated() {},
62-
beforeUnmount() {}, // new
63+
beforeUnmount() {}, // 追加
6364
unmounted() {}
6465
}
6566
```
6667

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

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

7374
```js
@@ -80,21 +81,21 @@ app.directive('highlight', {
8081
})
8182
```
8283

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

85-
### Edge Case: Accessing the component instance
86+
### 特別な問題への対処: コンポーネントのインスタンスへのアクセス
8687

87-
It's generally recommended to keep directives independent of the component instance they are used in. Accessing the instance from within a custom directive is often a sign that the directive should rather be a component itself. However, there are situations where this actually makes sense.
88+
一般的には、ディレクティブをコンポーネントのインスタンスから独立させることが推奨されています。カスタムディレクティブ内からインスタンスにアクセスする状況は、本来はディレクティブがコンポーネント自体であるべきことが多いです。しかし、まれにこれが有効なこともあります。
8889

89-
In Vue 2, the component instance had to be accessed through the `vnode` argument:
90+
Vue 2 では、コンポーネントインスタンスは `vnode` 引数を使ってアクセスする必要がありました。
9091

9192
```javascript
9293
bind(el, binding, vnode) {
9394
const vm = vnode.context
9495
}
9596
```
9697

97-
In Vue 3, the instance is now part of the `binding`:
98+
Vue 3 では、インスタンスは `binding` の一部になりました。
9899

99100
```javascript
100101
mounted(el, binding, vnode) {
@@ -103,5 +104,5 @@ mounted(el, binding, vnode) {
103104
```
104105

105106
:::warning
106-
With [fragments](/guide/migration/fragments.html#overview) support, components can potentially have more than one root nodes. When applied to a multi-root component, directive will be ignored and the warning will be thrown.
107+
[fragments](/guide/migration/fragments.html#overview) のサポートにより、コンポーネントは複数のルートノードを持つ可能性があります。マルチルートコンポーネントに適用すると、ディレクティブは無視され、警告がスローされます。
107108
:::

0 commit comments

Comments
 (0)