Skip to content

Commit 06fd0cd

Browse files
authored
translate guide/essentials/computed.md (#304)
1 parent bbe7dd9 commit 06fd0cd

File tree

3 files changed

+47
-47
lines changed

3 files changed

+47
-47
lines changed

.vitepress/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export const sidebar = {
130130
link: '/guide/essentials/reactivity-fundamentals'
131131
},
132132
{
133-
text: 'Computed Properties',
133+
text: '算出プロパティ',
134134
link: '/guide/essentials/computed'
135135
},
136136
{

.vitepress/theme/components/VueSchoolLink.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
rel="sponsored noopener"
77
:title="title"
88
>
9-
<slot>Watch a free video lesson on Vue School</slot>
9+
<slot>Vue School の無料動画レッスンを見る</slot>
1010
</a>
1111
</div>
1212
</template>

src/guide/essentials/computed.md

+45-45
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Computed Properties
1+
# 算出プロパティ
22

33
<div class="options-api">
4-
<VueSchoolLink href="https://vueschool.io/lessons/computed-properties-in-vue-3" title="Free Vue.js Computed Properties Lesson"/>
4+
<VueSchoolLink href="https://vueschool.io/lessons/computed-properties-in-vue-3" title="Vue School の算出プロパティの無料動画レッスン"/>
55
</div>
66

77
<div class="composition-api">
8-
<VueSchoolLink href="https://vueschool.io/lessons/vue-fundamentals-capi-computed-properties-in-vue-with-the-composition-api" title="Free Vue.js Computed Properties Lesson"/>
8+
<VueSchoolLink href="https://vueschool.io/lessons/vue-fundamentals-capi-computed-properties-in-vue-with-the-composition-api" title="Vue School の算出プロパティの無料動画レッスン"/>
99
</div>
1010

11-
## Basic Example
11+
## 基本的な例
1212

13-
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:
13+
テンプレート内に式を書けるのはとても便利ですが、非常に簡単な操作しかできません。テンプレート内に多くのロジックを詰め込むと、コードが肥大化し、メンテナンスが難しくなります。例えば、配列が入れ子になっているオブジェクトがあった場合:
1414

1515
<div class="options-api">
1616

@@ -47,16 +47,16 @@ const author = reactive({
4747

4848
</div>
4949

50-
And we want to display different messages depending on if `author` already has some books or not:
50+
そして、`author` がすでにいくつかの `books` を持っているかどうかによって、異なるメッセージを表示したいとします:
5151

5252
```vue-html
5353
<p>Has published books:</p>
5454
<span>{{ author.books.length > 0 ? 'Yes' : 'No' }}</span>
5555
```
5656

57-
At this point, the template is getting a bit cluttered. We have to look at it for a second before realizing that it performs a calculation depending on `author.books`. More importantly, we probably don't want to repeat ourselves if we need to include this calculation in the template more than once.
57+
この時点で、テンプレートが少しごちゃごちゃしてきました。しばらく眺めて、やっとこれが `author.books` に依存した計算をしていることに気づくでしょう。さらに重要なことは、同じ計算をテンプレートの中で複数回使う場合、おそらく繰り返して使いたくはないでしょう。
5858

59-
That's why for complex logic that includes reactive data, it is recommended to use a **computed property**. Here's the same example, refactored:
59+
上記の理由から、リアクティブなデータを含む複雑なロジックには**算出プロパティ**を使用すべきです。以下は上記と同じ例をリファクタリングしたものです:
6060

6161
<div class="options-api">
6262

@@ -75,9 +75,9 @@ export default {
7575
}
7676
},
7777
computed: {
78-
// a computed getter
78+
// 算出プロパティの getter 関数
7979
publishedBooksMessage() {
80-
// `this` points to the component instance
80+
// `this` はコンポーネントのインスタンスを指します
8181
return this.author.books.length > 0 ? 'Yes' : 'No'
8282
}
8383
}
@@ -89,15 +89,15 @@ export default {
8989
<span>{{ publishedBooksMessage }}</span>
9090
```
9191

92-
[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdD5cbmV4cG9ydCBkZWZhdWx0IHtcbiAgZGF0YSgpIHtcbiAgICByZXR1cm4ge1xuICAgICAgYXV0aG9yOiB7XG4gICAgICAgIG5hbWU6ICdKb2huIERvZScsXG4gICAgICAgIGJvb2tzOiBbXG4gICAgICAgICAgJ1Z1ZSAyIC0gQWR2YW5jZWQgR3VpZGUnLFxuICAgICAgICAgICdWdWUgMyAtIEJhc2lj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oID4gMCA/ICdZZXMnIDogJ05vJ1xuICAgIH1cbiAgfVxufVxuPC9zY3JpcHQ+XG5cbjx0ZW1wbGF0ZT5cbiAgPHA+SGFzIHB1Ymxpc2hlZCBib29rczo8L3A+XG4gIDxzcGFuPnt7IGF1dGhvci5ib29rcy5sZW5ndGggPiAwID8gJ1llcycgOiAnTm8nIH19PC9zcGFuPlxuPC90ZW1wbGF0ZT4iLCJpbXBvcnQtbWFwLmpzb24iOiJ7XG4gIFwiaW1wb3J0c1wiOiB7XG4gICAgXCJ2dWVcIjogXCJodHRwczovL3NmYy52dWVqcy5vcmcvdnVlLnJ1bnRpbWUuZXNtLWJyb3dzZXIuanNcIlxuICB9XG59In0=)
9393

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

96-
Try to change the value of the `books` array in the application `data` and you will see how `publishedBooksMessage` is changing accordingly.
96+
アプリケーションの `data` プロパティ内の `books` 配列の値を変更してみると、それに応じて `publishedBooksMessage` の結果がどのように変化しているかがわかります。
9797

98-
You can data-bind to computed properties in templates just like a normal property. Vue is aware that `this.publishedBooksMessage` depends on `this.author.books`, so it will update any bindings that depend on `this.publishedBooksMessage` when `this.author.books` changes.
98+
通常のプロパティと同じように、テンプレート内の算出プロパティにデータバインドすることもできます。Vue `this.publishedBooksMessage` `this.author.books` に依存していることを知っているので、`this.author.books` が変わると `this.publishedBooksMessage` に依存する全てのバインディングを更新します。
9999

100-
See also: [Typing Computed Properties](/guide/typescript/options-api.html#typing-computed-properties) <sup class="vt-badge ts" />
100+
参照: [Typing Computed Properties](/guide/typescript/options-api.html#typing-computed-properties) <sup class="vt-badge ts" />
101101

102102
</div>
103103

@@ -116,7 +116,7 @@ const author = reactive({
116116
]
117117
})
118118
119-
// a computed ref
119+
// 算出プロパティの参照
120120
const publishedBooksMessage = computed(() => {
121121
return author.books.length > 0 ? 'Yes' : 'No'
122122
})
@@ -128,19 +128,19 @@ const publishedBooksMessage = computed(() => {
128128
</template>
129129
```
130130

131-
[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlYWN0aXZlLCBjb21wdXRlZCB9IGZyb20gJ3Z1ZSdcblxuY29uc3QgYXV0aG9yID0gcmVhY3RpdmUoe1xuICBuYW1lOiAnSm9obiBEb2UnLFxuICBib29rczogW1xuICAgICdWdWUgMiAtIEFkdmFuY2VkIEd1aWRlJyxcbiAgICAnVnVlIDMgLSBCYXNpYyBHdWlkZScsXG4gICAgJ1Z1ZSA0IC0gVGhlIE15c3RlcnknXG4gIF1cbn0pXG5cbi8vIGEgY29tcHV0ZWQgcmVmXG5jb25zdCBwdWJsaXNoZWRCb29rc01lc3NhZ2UgPSBjb21wdXRlZCgoKSA9PiB7XG4gIHJldHVybiBhdXRob3IuYm9va3MubGVuZ3RoID4gMCA/ICdZZXMnIDogJ05vJ1xufSlcbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU+XG4gIDxwPkhhcyBwdWJsaXNoZWQgYm9va3M6PC9wPlxuICA8c3Bhbj57eyBwdWJsaXNoZWRCb29rc01lc3NhZ2UgfX08L3NwYW4+XG48L3RlbXBsYXRlPiIsImltcG9ydC1tYXAuanNvbiI6IntcbiAgXCJpbXBvcnRzXCI6IHtcbiAgICBcInZ1ZVwiOiBcImh0dHBzOi8vc2ZjLnZ1ZWpzLm9yZy92dWUucnVudGltZS5lc20tYnJvd3Nlci5qc1wiXG4gIH1cbn0ifQ==)
131+
[プレイグラウンドで試す](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlYWN0aXZlLCBjb21wdXRlZCB9IGZyb20gJ3Z1ZSdcblxuY29uc3QgYXV0aG9yID0gcmVhY3RpdmUoe1xuICBuYW1lOiAnSm9obiBEb2UnLFxuICBib29rczogW1xuICAgICdWdWUgMiAtIEFkdmFuY2VkIEd1aWRlJyxcbiAgICAnVnVlIDMgLSBCYXNpYyBHdWlkZScsXG4gICAgJ1Z1ZSA0IC0gVGhlIE15c3RlcnknXG4gIF1cbn0pXG5cbi8vIGEgY29tcHV0ZWQgcmVmXG5jb25zdCBwdWJsaXNoZWRCb29rc01lc3NhZ2UgPSBjb21wdXRlZCgoKSA9PiB7XG4gIHJldHVybiBhdXRob3IuYm9va3MubGVuZ3RoID4gMCA/ICdZZXMnIDogJ05vJ1xufSlcbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU+XG4gIDxwPkhhcyBwdWJsaXNoZWQgYm9va3M6PC9wPlxuICA8c3Bhbj57eyBwdWJsaXNoZWRCb29rc01lc3NhZ2UgfX08L3NwYW4+XG48L3RlbXBsYXRlPiIsImltcG9ydC1tYXAuanNvbiI6IntcbiAgXCJpbXBvcnRzXCI6IHtcbiAgICBcInZ1ZVwiOiBcImh0dHBzOi8vc2ZjLnZ1ZWpzLm9yZy92dWUucnVudGltZS5lc20tYnJvd3Nlci5qc1wiXG4gIH1cbn0ifQ==)
132132

133-
Here we have declared a computed property `publishedBooksMessage`. The `computed()` function expects to be passed a getter function, and the returned value is a **computed ref**. Similar to normal refs, you can access the computed result as `publishedBooksMessage.value`. Computed refs are also auto-unwrapped in templates so you can reference them without `.value` in template expressions.
133+
ここでは、`publishedBooksMessage` という算出プロパティを宣言しています。`computed()` 関数は getter 関数が渡されることを想定しており、返り値は **算出された ref** となります。通常の ref と同様に、`publishedBooksMessage.value` で算出結果を参照することができます。また、算出結果はテンプレート内では自動的にアンラップされるため、テンプレート内では `.value` なしで参照することができます。
134134

135-
A computed property automatically tracks its reactive dependencies. Vue is aware that the computation of `publishedBooksMessage` depends on `author.books`, so it will update any bindings that depend on `publishedBooksMessage` when `author.books` changes.
135+
算出プロパティは、自動的にリアクティブな依存関係を追跡します。Vue `publishedBooksMessage` の算出が `author.books` に依存することを知っているので、`author.books` が変わると `publishedBooksMessage` に依存する全てのバインディングを更新します。
136136

137-
See also: [Typing Computed](/guide/typescript/composition-api.html#typing-computed) <sup class="vt-badge ts" />
137+
参考: [Typing Computed](/guide/typescript/composition-api.html#typing-computed) <sup class="vt-badge ts" />
138138

139139
</div>
140140

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

143-
You may have noticed we can achieve the same result by invoking a method in the expression:
143+
こういった式を持つメソッドを呼び出すことで、同じ結果が実現できることに気付いたかもしれません:
144144

145145
```vue-html
146146
<p>{{ calculateBooksMessage() }}</p>
@@ -149,7 +149,7 @@ You may have noticed we can achieve the same result by invoking a method in the
149149
<div class="options-api">
150150

151151
```js
152-
// in component
152+
// コンポーネント内
153153
methods: {
154154
calculateBooksMessage() {
155155
return this.author.books.length > 0 ? 'Yes' : 'No'
@@ -162,17 +162,17 @@ methods: {
162162
<div class="composition-api">
163163

164164
```js
165-
// in component
165+
// コンポーネント内
166166
function calculateBooksMessage() {
167167
return author.books.length > 0 ? 'Yes' : 'No'
168168
}
169169
```
170170

171171
</div>
172172

173-
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 `publishedBooksMessage` will immediately return the previously computed result without having to run the getter function again.
173+
算出プロパティの代わりに、同じような関数をメソッドとして定義することもできます。最終的には、2 つのアプローチは完全に同じ結果になります。しかしながら、**算出プロパティはリアクティブな依存関係にもとづきキャッシュされる**という違いがあります。算出プロパティは、リアクティブな依存関係が更新されたときにだけ再評価されます。これはつまり、 `author.books` が変わらない限りは、`publishedBooksMessage` に何度アクセスしても、getter 関数を再び実行することなく、以前計算された結果を即時に返すということです。
174174

175-
This also means the following computed property will never update, because `Date.now()` is not a reactive dependency:
175+
`Date.now()` はリアクティブな依存ではないため、次の算出プロパティは二度と更新されないことを意味します:
176176

177177
<div class="options-api">
178178

@@ -194,13 +194,13 @@ const now = computed(() => Date.now())
194194

195195
</div>
196196

197-
In comparison, a method invocation will **always** run the function whenever a re-render happens.
197+
対称的に、メソッド呼び出しは、再描画が起きると**常に**関数を実行します。
198198

199-
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 call instead.
199+
なぜキャッシングが必要なのでしょうか?巨大な配列をループしたり多くの計算を必要とする、コストの高い `list` という算出プロパティがあることを想像してみてください。`list` に依存する他の算出プロパティもあるかもしれません。その場合、キャッシングがなければ必要以上に `list`getter を実行することになってしまいます。キャッシングしたくない場合は、代わりにメソッドを使いましょう。
200200

201-
## Writable Computed
201+
## 書き込み可能な 算出関数
202202

203-
Computed properties are by default getter-only. If you attempt to assign a new value to a computed property, you will receive a runtime warning. In the rare cases where you need a "writable" computed property, you can create one by providing both a getter and a setter:
203+
算出プロパティは、デフォルトでは getter 関数のみです。算出プロパティに新しい値を代入しようとすると、ランタイム警告が表示されます。まれに「書き込み可能な」算出プロパティが必要な場合があります。その場合は getter 関数と setter 関数の両方を提供することで、それを作成することができます:
204204

205205
<div class="options-api">
206206

@@ -214,21 +214,21 @@ export default {
214214
},
215215
computed: {
216216
fullName: {
217-
// getter
217+
// getter 関数
218218
get() {
219219
return this.firstName + ' ' + this.lastName
220220
},
221-
// setter
221+
// setter 関数
222222
set(newValue) {
223-
// Note: we are using destructuring assignment syntax here.
224-
[this.firstName, this.lastName] = newValue.split(' ')
223+
// 注意: ここでは、破壊的な代入構文を使用しています。
224+
;[this.firstName, this.lastName] = newValue.split(' ')
225225
}
226226
}
227227
}
228228
}
229229
```
230230

231-
Now when you run `this.fullName = 'John Doe'`, the setter will be invoked and `this.firstName` and `this.lastName` will be updated accordingly.
231+
`this.fullName = 'John Doe'` を呼ぶと、setter 関数が呼び出され、`this.firstName` `this.lastName` が適切に更新されます。
232232

233233
</div>
234234

@@ -242,29 +242,29 @@ const firstName = ref('John')
242242
const lastName = ref('Doe')
243243
244244
const fullName = computed({
245-
// getter
245+
// getter 関数
246246
get() {
247247
return firstName.value + ' ' + lastName.value
248248
},
249-
// setter
249+
// setter 関数
250250
set(newValue) {
251-
// Note: we are using destructuring assignment syntax here.
252-
[firstName.value, lastName.value] = newValue.split(' ')
251+
// 注意: ここでは、破壊的な代入構文を使用しています。
252+
;[firstName.value, lastName.value] = newValue.split(' ')
253253
}
254254
})
255255
</script>
256256
```
257257

258-
Now when you run `fullName.value = 'John Doe'`, the setter will be invoked and `firstName` and `lastName` will be updated accordingly.
258+
`fullName = 'John Doe'` を呼ぶと、setter 関数が呼び出され、`firstName` `lastName` が適切に更新されます。
259259

260260
</div>
261261

262-
## Best Practices
262+
## ベストプラクティス
263263

264-
### Getters should be side-effect free
264+
### getter 関数は副作用のないものでなければならない
265265

266-
It is important to remember that computed getter functions should only perform pure computation and be free of side effects. For example, don't make async requests or mutate the DOM inside a computed getter! Think of a computed property as declaratively describing how to derive a value based on other values - its only responsibility should be computing and returning that value. Later in the guide we will discuss how we can perform side effects in reaction to state changes with [watchers](./watchers).
266+
算出プロパティにおける getter 関数は計算のみを行い、副作用がないようにすることが重要です。例えば、非同期リクエストや、DOM を変化させないようにしましょう!算出プロパティは他の値に基づいて計算する方法を宣言的に記述していると考えてください。その唯一の責任は、値を計算して返すことでなければなりません。このガイドの後半では、 [ウォッチャー](./watchers) を使って、状態の変化に反応して副作用を実行する方法について説明します。
267267

268-
### Avoid mutating computed value
268+
### 算出した値の変更を避ける
269269

270-
The returned value from a computed property is derived state. Think of it as a temporary snapshot - every time the source state changes, a new snapshot is created. It does not make sense to mutate a snapshot, so a computed return value should be treated as read-only and never be mutated - instead, update the source state it depends on to trigger new computations.
270+
算出プロパティから返る値は、派生され状態です。一時的なスナップショットとして考えてください。ソースの状態が変わるたびに、新しいスナップショットが作成されます。スナップショットの値を変更することは意味がないため、計算された結果は読み取り専用として扱い、変更しないようにします。その代わり、新しい計算結果が必要な場合は、依存するソースの状態を更新します。

0 commit comments

Comments
 (0)