Skip to content

Commit 8c2e759

Browse files
authored
Essentials > Computed Properties and Watchers の翻訳 (#72)
* docs:translate guide/computed.md * fix:translate guide/computed.md
1 parent 5821c03 commit 8c2e759

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

src/guide/computed.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Computed Properties and Watchers
1+
# 算出プロパティとウォッチャ
22

3-
## Computed Properties
3+
## 算出プロパティ
44

5-
In-template expressions are very convenient, but they are meant for simple operations. Putting too much logic in your templates can make them bloated and hard to maintain. For example, if we have an object with a nested array:
5+
テンプレート内の式は非常に便利ですが、シンプルな操作のためのものです。テンプレートにたくさんロジックを入れすぎると、テンプレートが肥大化してメンテナンスしづらくなる可能性があります。たとえば、配列が入っているオブジェクトがあり:
66

77
```js
88
Vue.createApp({
@@ -21,7 +21,7 @@ Vue.createApp({
2121
})
2222
```
2323

24-
And we want to display different messages depending on if `author` already has some books or not
24+
`author` が本を持っているかどうかによって違うメッセージを表示しようとしています。
2525

2626
```html
2727
<div id="computed-basics">
@@ -30,11 +30,11 @@ And we want to display different messages depending on if `author` already has s
3030
</div>
3131
```
3232

33-
At this point, the template is no longer simple and declarative. You have to look at it for a second before realizing that it performs a calculation depending on `author.books`. The problem is made worse when you want to include this calculation in your template more than once.
33+
これだと、テンプレートはもうシンプルでも宣言的でもありません。`author.books` を元に計算していると分かるまで少し時間がかかります。この計算を何回もテンプレートに含めたい場合、問題はさらに悪化します。
3434

35-
That's why for complex logic that includes reactive data, you should use a **computed property**.
35+
そのため、リアクティブなデータを含む複雑なロジックには**算出プロパティ**を使うべきです。
3636

37-
### Basic Example
37+
### 基本的な例
3838

3939
```html
4040
<div id="computed-basics">
@@ -58,16 +58,16 @@ Vue.createApp({
5858
}
5959
},
6060
computed: {
61-
// a computed getter
61+
// 算出 getter 関数
6262
publishedBooksMessage() {
63-
// `this` points to the vm instance
63+
// `this` vm インスタンスを指す
6464
return this.author.books.length > 0 ? 'Yes' : 'No'
6565
}
6666
}
6767
}).mount('#computed-basics')
6868
```
6969

70-
Result:
70+
結果:
7171

7272
<p class="codepen" data-height="300" data-theme-id="39028" data-default-tab="js,result" data-user="Vue" data-slug-hash="NWqzrjr" data-editable="true" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Computed basic example">
7373
<span>See the Pen <a href="https://codepen.io/team/Vue/pen/NWqzrjr">
@@ -76,32 +76,32 @@ Result:
7676
</p>
7777
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
7878

79-
Here we have declared a computed property `publishedBooksMessage`.
79+
ここでは `publishedBooksMessage` という算出プロパティを宣言しています。
8080

81-
Try to change the value of `books` array in the application `data` and you will see how `publishedBooksMessage` is changing accordingly.
81+
このアプリケーションの `data` にある `books` 配列の値を変更してみると、それに応じて `publishedBooksMessage` がどのように変わるか分かるでしょう。
8282

83-
You can data-bind to computed properties in templates just like a normal property. Vue is aware that `vm.publishedBooksMessage` depends on `vm.author.books`, so it will update any bindings that depend on `vm.publishedBooksMessage` when `vm.author.books` changes. And the best part is that we've created this dependency relationship declaratively: the computed getter function has no side effects, which makes it easier to test and understand.
83+
通常のプロパティと同じように、テンプレート内で算出プロパティにデータバインドできます。Vue `vm.publishedBooksMessage` `vm.author.books` に依存していると分かっているので、`vm.author.books` が変更されると `vm.publishedBooksMessage` に依存するバインディングを更新します。そして、一番よいのは、この依存関係を宣言的に作成していることです。算出 getter 関数には副作用がないので、テストや理解するのが容易になります。
8484

85-
### Computed Caching vs Methods
85+
### 算出プロパティ vs メソッド
8686

87-
You may have noticed we can achieve the same result by invoking a method in the expression:
87+
式の中でメソッドを呼び出せば同じことができると気づいたかもしれません:
8888

8989
```html
9090
<p>{{ calculateBooksMessage() }}</p>
9191
```
9292

9393
```js
94-
// in component
94+
// コンポーネントの中
9595
methods: {
9696
calculateBooksMessage() {
9797
return this.author.books.length > 0 ? 'Yes' : 'No'
9898
}
9999
}
100100
```
101101

102-
Instead of a computed property, we can define the same function as a method. For the end result, the two approaches are indeed exactly the same. However, the difference is that **computed properties are cached based on their reactive dependencies.** A computed property will only re-evaluate when some of its reactive dependencies have changed. This means as long as `author.books` has not changed, multiple access to the `publishedBooksMessage` computed property will immediately return the previously computed result without having to run the function again.
102+
同じ関数を算出プロパティではなくメソッドとして定義することもできます。結果だけ見れば、この2つのアプローチはまったく同じになりますが、**算出プロパティはリアクティブな依存関係に基づいてキャッシュされる**という違いがあります。算出プロパティはリアクティブな依存関係の一部が変更された場合にのみ再評価されるのです。つまり、`author.books`が変更されなければ、算出プロパティの `publishedBooksMessage` に複数回アクセスしても関数は再実行されず、前回計算した結果がすぐに返されます。
103103

104-
This also means the following computed property will never update, because `Date.now()` is not a reactive dependency:
104+
以下の算出プロパティは `Date.now()` がリアクティブな依存関係ではないので、一度も更新されないことになります:
105105

106106
```js
107107
computed: {
@@ -111,23 +111,23 @@ computed: {
111111
}
112112
```
113113

114-
In comparison, a method invocation will **always** run the function whenever a re-render happens.
114+
それに対してメソッドは、再レンダリングが起こるたびに**常に**関数を実行します。
115115

116-
Why do we need caching? Imagine we have an expensive computed property `list`, which requires looping through a huge array and doing a lot of computations. Then we may have other computed properties that in turn depend on `list`. Without caching, we would be executing `list`’s getter many more times than necessary! In cases where you do not want caching, use a `method` instead.
116+
どうしてキャッシングが必要なのでしょうか? 巨大な配列をループして、たくさんの計算を必要とする算出プロパティ `list` があるとします。また、`list` に依存している別の算出プロパティがあるかも知れません。キャッシングがなければ、`list`getter 関数を必要以上に何度も実行することになってしまいます。キャッシングをしたくない場合は、代わりに `method` を使用してください。
117117

118-
### Computed Setter
118+
### 算出 Setter 関数
119119

120-
Computed properties are by default getter-only, but you can also provide a setter when you need it:
120+
算出プロパティはデフォルトでは getter 関数のみですが、必要に応じて setter 関数を設定することもできます:
121121

122122
```js
123123
// ...
124124
computed: {
125125
fullName: {
126-
// getter
126+
// getter 関数
127127
get() {
128128
return this.firstName + ' ' + this.lastName
129129
},
130-
// setter
130+
// setter 関数
131131
set(newValue) {
132132
const names = newValue.split(' ')
133133
this.firstName = names[0]
@@ -138,13 +138,13 @@ computed: {
138138
// ...
139139
```
140140

141-
Now when you run `vm.fullName = 'John Doe'`, the setter will be invoked and `vm.firstName` and `vm.lastName` will be updated accordingly.
141+
この状態で `vm.fullName = 'John Doe'` を実行すると setter 関数が呼び出され、その結果 `vm.firstName` `vm.lastName` が更新されます。
142142

143-
## Watchers
143+
## ウォッチャ
144144

145-
While computed properties are more appropriate in most cases, there are times when a custom watcher is necessary. That's why Vue provides a more generic way to react to data changes through the `watch` option. This is most useful when you want to perform asynchronous or expensive operations in response to changing data.
145+
ほとんどの場合、算出プロパティの方が適切ですが、カスタムウォッチャが必要な場合もあります。そのため Vue は、データの変更に反応するためのより汎用的な方法を、`watch` オプションによって提供しています。これはデータを変更するのに応じて非同期処理や重い処理を実行したい場合に最も便利です。
146146

147-
For example:
147+
:
148148

149149
```html
150150
<div id="watch-example">
@@ -157,10 +157,10 @@ For example:
157157
```
158158

159159
```html
160-
<!-- Since there is already a rich ecosystem of ajax libraries -->
161-
<!-- and collections of general-purpose utility methods, Vue core -->
162-
<!-- is able to remain small by not reinventing them. This also -->
163-
<!-- gives you the freedom to use what you're familiar with. -->
160+
<!-- ajax ライブラリや汎用ユーティリティメソッドのコレクションなどの -->
161+
<!-- 豊富なエコシステムがすでに存在するため、それらを再発明しないことで -->
162+
<!-- Vue のコアは小規模なまま保たれています。これは、使い慣れたものを -->
163+
<!-- 自由に使うことができる、ということでもあります。 -->
164164
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js"></script>
165165
<script>
166166
const watchExampleVM = Vue.createApp({
@@ -171,7 +171,7 @@ For example:
171171
}
172172
},
173173
watch: {
174-
// whenever question changes, this function will run
174+
// question が変わるたびに、この関数が実行される
175175
question(newQuestion, oldQuestion) {
176176
if (newQuestion.indexOf('?') > -1) {
177177
this.getAnswer()
@@ -195,7 +195,7 @@ For example:
195195
</script>
196196
```
197197

198-
Result:
198+
結果:
199199

200200
<p class="codepen" data-height="300" data-theme-id="39028" data-default-tab="result" data-user="Vue" data-slug-hash="GRJGqXp" data-editable="true" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Watch basic example">
201201
<span>See the Pen <a href="https://codepen.io/team/Vue/pen/GRJGqXp">
@@ -204,13 +204,13 @@ Result:
204204
</p>
205205
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
206206

207-
In this case, using the `watch` option allows us to perform an asynchronous operation (accessing an API) and sets a condition for performing this operation. None of that would be possible with a computed property.
207+
このケースでは `watch` オプションを使用することで、非同期処理(API へのアクセス)の実行と、その処理を実行する条件を設定できています。このようなことは算出プロパティではできません。
208208

209-
In addition to the `watch` option, you can also use the imperative [vm.\$watch API](../api/instance-methods.html#watch).
209+
`watch` オプションに加え、命令的な [vm.\$watch API](../api/instance-methods.html#watch) を使うこともできます。
210210

211-
### Computed vs Watched Property
211+
### 算出プロパティ vs 監視プロパティ
212212

213-
Vue does provide a more generic way to observe and react to data changes on a current active instance: **watch properties**. When you have some data that needs to change based on some other data, it is tempting to overuse `watch` - especially if you are coming from an AngularJS background. However, it is often a better idea to use a computed property rather than an imperative `watch` callback. Consider this example:
213+
Vue は現在のアクティブなインスタンスでのデータの変更を観察して反応するための、より汎用的な方法: **監視プロパティ(watched property)** を提供します。他のデータに基づいて変更しなければならないデータがある場合(AngularJS をやっていた人だとなおさら)、`watch` を使いすぎてしまいがちです。しかし、たいていの場合は命令的な `watch` のコールバックよりも算出プロパティを使うのがベターです。次の例を考えてみましょう:
214214

215215
```html
216216
<div id="demo">{{ fullName }}</div>
@@ -236,7 +236,7 @@ const vm = Vue.createApp({
236236
}).mount('#demo')
237237
```
238238

239-
The above code is imperative and repetitive. Compare it with a computed property version:
239+
上記のコードは命令的だし冗長ですね。算出プロパティで書き換えたものと比べてみましょう:
240240

241241
```js
242242
const vm = Vue.createApp({
@@ -254,4 +254,4 @@ const vm = Vue.createApp({
254254
}).mount('#demo')
255255
```
256256

257-
Much better, isn't it?
257+
ずっといいですよね?

0 commit comments

Comments
 (0)