Skip to content

Commit 8e334a3

Browse files
docs: translate guide/change-detection.md (#77)
1 parent cdf358a commit 8e334a3

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

src/guide/change-detection.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
1-
# Change Detection Caveats in Vue 2
1+
# Vue 2 での変更検出の注意事項
22

3-
> This page applies only to Vue 2.x and below, and assumes you've already read the [Reactivity Section](reactivity.md). Please read that section first.
3+
> このページは Vue 2.x 以下にのみ適用され、すでに[リアクティブの探求](reactivity.md)を読んでいることが前提です。最初にそのセクションを読んでください。
44
5-
Due to limitations in JavaScript, there are types of changes that Vue **cannot detect**. However, there are ways to circumvent them to preserve reactivity.
5+
JavaScript の制限のため、Vue は、**検出することができない**変更のタイプがあります。しかし、それらを回避しリアクティビティを維持する方法はあります。
66

7-
### For Objects
7+
### オブジェクトに関して
88

9-
Vue cannot detect property addition or deletion. Since Vue performs the getter/setter conversion process during instance initialization, a property must be present in the `data` object in order for Vue to convert it and make it reactive. For example:
9+
Vue はプロパティの追加または削除を検出できません。Vue はインスタンスの初期化中に getter/setter の変換を行うため、全てのプロパティは Vue が変換してリアクティブにできるように `data` オブジェクトに存在しなければなりません。例えば:
1010

1111
```js
1212
var vm = new Vue({
1313
data: {
1414
a: 1
1515
}
1616
})
17-
// `vm.a` is now reactive
17+
// `vm.a` は今リアクティブです
1818

1919
vm.b = 2
20-
// `vm.b` is NOT reactive
20+
// `vm.b` はリアクティブでは"ありません"
2121
```
2222

23-
Vue does not allow dynamically adding new root-level reactive properties to an already created instance. However, it's possible to add reactive properties to a nested object using the `Vue.set(object, propertyName, value)` method:
23+
Vue では、すでに作成されたインスタンスに対して新しいルートレベルのリアクティブなプロパティを動的に追加することはできません。しかしながら、`Vue.set(object, propertyName, value)` メソッドを使ってネストしたオブジェクトにリアクティブなプロパティを追加することができます:
2424

2525
```js
2626
Vue.set(vm.someObject, 'b', 2)
2727
```
2828

29-
You can also use the `vm.$set` instance method, which is an alias to the global `Vue.set`:
29+
`vm.$set` インスタンスメソッドを使用することもできます。これはグローバルの `Vue.set` のエイリアスです:
3030

3131
```js
3232
this.$set(this.someObject, 'b', 2)
3333
```
3434

35-
Sometimes you may want to assign a number of properties to an existing object, for example using `Object.assign()` or `_.extend()`. However, new properties added to the object will not trigger changes. In such cases, create a fresh object with properties from both the original object and the mixin object:
35+
既存のオブジェクトに複数のプロパティを割り当てたいということがあるかもしれません。例えば、`Object.assign()` `_.extend()` を使って。しかし、オブジェクトに追加された新しいプロパティは変更をトリガしません。このような場合は、元のオブジェクトとミックスインオブジェクトの両方のプロパティを持つ新たなオブジェクトを作成してください:
3636

3737
```js
38-
// instead of `Object.assign(this.someObject, { a: 1, b: 2 })`
38+
// `Object.assign(this.someObject, { a: 1, b: 2 })` の代わり
3939
this.someObject = Object.assign({}, this.someObject, { a: 1, b: 2 })
4040
```
4141

42-
### For Arrays
42+
### 配列に関して
4343

44-
Vue cannot detect the following changes to an array:
44+
Vue は、配列における次の変更は検知できません:
4545

46-
1. When you directly set an item with the index, e.g. `vm.items[indexOfItem] = newValue`
47-
2. When you modify the length of the array, e.g. `vm.items.length = newLength`
46+
1. インデックスと一緒にアイテムを直接セットする場合、例えば `vm.items[indexOfItem] = newValue`
47+
2. 配列の長さを変更する場合、例えば `vm.items.length = newLength`
4848

49-
For example:
49+
:
5050

5151
```js
5252
var vm = new Vue({
5353
data: {
5454
items: ['a', 'b', 'c']
5555
}
5656
})
57-
vm.items[1] = 'x' // is NOT reactive
58-
vm.items.length = 2 // is NOT reactive
57+
vm.items[1] = 'x' // リアクティブでは"ありません"
58+
vm.items.length = 2 // リアクティブでは"ありません"
5959
```
6060

61-
To overcome caveat 1, both of the following will accomplish the same as `vm.items[indexOfItem] = newValue`, but will also trigger state updates in the reactivity system:
61+
注意事項 1 を克服するに、次のいずれも `vm.items[indexOfItem] = newValue` と同じように機能しますが、リアクティビティシステムで状態の更新をトリガします:
6262

6363
```js
6464
// Vue.set
@@ -70,43 +70,43 @@ Vue.set(vm.items, indexOfItem, newValue)
7070
vm.items.splice(indexOfItem, 1, newValue)
7171
```
7272

73-
You can also use the [`vm.$set`](https://vuejs.org/v2/api/#vm-set) instance method, which is an alias for the global `Vue.set`:
73+
また、[`vm.$set`](https://vuejs.org/v2/api/#vm-set) インスタンスメソッドを使うこともできます。これはグローバルな `Vue.set` のエイリアスです:
7474

7575
```js
7676
vm.$set(vm.items, indexOfItem, newValue)
7777
```
7878

79-
To deal with caveat 2, you can use `splice`:
79+
注意事項 2 に対応するには、`splice` を使うことができます:
8080

8181
```js
8282
vm.items.splice(newLength)
8383
```
8484

85-
## Declaring Reactive Properties
85+
## リアクティブプロパティの宣言
8686

87-
Since Vue doesn't allow dynamically adding root-level reactive properties, you have to initialize component instances by declaring all root-level reactive data properties upfront, even with an empty value:
87+
Vue では新しいルートレベルのリアクティブなプロパティを動的に追加することはできないため、インスタンスの初期化時に前もって全てのルートレベルのリアクティブな data プロパティを宣言する必要があります。空の値でもかまいません:
8888

8989
```js
9090
var vm = new Vue({
9191
data: {
92-
// declare message with an empty value
92+
// 空の値として message を宣言する
9393
message: ''
9494
},
9595
template: '<div>{{ message }}</div>'
9696
})
97-
// set `message` later
97+
// 後で`message` を追加する
9898
vm.message = 'Hello!'
9999
```
100100

101-
If you don't declare `message` in the data option, Vue will warn you that the render function is trying to access a property that doesn't exist.
101+
data オプションで `message` を宣言していないと、Vue render 関数が存在しないプロパティにアクセスしようとしていることを警告します。
102102

103-
There are technical reasons behind this restriction - it eliminates a class of edge cases in the dependency tracking system, and also makes component instances play nicer with type checking systems. But there is also an important consideration in terms of code maintainability: the `data` object is like the schema for your component's state. Declaring all reactive properties upfront makes the component code easier to understand when revisited later or read by another developer.
103+
この制限の背後には技術的な理由があります。それは依存性追跡システムにおける一連のエッジケースを排除し、また Vue インスタンスと型チェックシステムとの親和性を高めます。しかしコードの保守性の観点からも重要な事項があります: `data` オブジェクトはコンポーネントの状態のスキーマのようなものです。前もって全てのリアクティブなプロパティを宣言しておくと、後から見直したり別の開発者が読んだりしたときにコンポーネントのコードを簡単に理解することができます。
104104

105-
## Async Update Queue
105+
## 非同期更新キュー
106106

107-
In case you haven't noticed yet, Vue performs DOM updates **asynchronously**. Whenever a data change is observed, it will open a queue and buffer all the data changes that happen in the same event loop. If the same watcher is triggered multiple times, it will be pushed into the queue only once. This buffered de-duplication is important in avoiding unnecessary calculations and DOM manipulations. Then, in the next event loop "tick", Vue flushes the queue and performs the actual (already de-duped) work. Internally Vue tries native `Promise.then`, `MutationObserver`, and `setImmediate` for the asynchronous queuing and falls back to `setTimeout(fn, 0)`.
107+
おそらく既にお気づきでしょうが、Vue **非同期** に DOM 更新を実行します。データ変更が検出されると、Vue はキューをオープンし、同じイベントループで起こる全てのデータ変更をバッファリングします。同じウォッチャが複数回トリガされる場合、キューには一度だけ入ります。この重複除外バッファリングは不要な計算や DOM 操作を回避する上で重要です。そして、次のイベントループ "tick" で、Vue はキューをフラッシュし、実際の(すでに重複が除外された)作業を実行します。内部的には、Vue は非同期キューイング向けにネイティブな `Promise.then``MutationObserver``setImmediate` が利用可能ならそれを使い、`setTimeout(fn, 0)` にフォールバックします。
108108

109-
For example, when you set `vm.someData = 'new value'`, the component will not re-render immediately. It will update in the next "tick", when the queue is flushed. Most of the time we don't need to care about this, but it can be tricky when you want to do something that depends on the post-update DOM state. Although Vue.js generally encourages developers to think in a "data-driven" fashion and avoid touching the DOM directly, sometimes it might be necessary to get your hands dirty. In order to wait until Vue.js has finished updating the DOM after a data change, you can use `Vue.nextTick(callback)` immediately after the data is changed. The callback will be called after the DOM has been updated. For example:
109+
例として、`vm.someData = 'new value'` をセットした時、そのコンポーネントはすぐには再描画しません。 次の "tick" でキューがフラッシュされる時に更新します。ほとんどの場合、私達はこれについて気にする必要はありませんが、更新した DOM の状態に依存する何かをしたい時は注意が必要です。Vue.js は一般的に"データ駆動"的な流儀で考え、DOM を直接触るのを避けることを開発者に奨励しますが、時にはその手を汚す必要があるかもしれません。データの変更後に Vue.js DOM 更新の完了を待つには、データが変更された直後に `Vue.nextTick(callback)` を使用することができます。そのコールバックは DOM が更新された後に呼ばれます。例えば:
110110

111111
```html
112112
<div id="example">{{ message }}</div>
@@ -119,14 +119,14 @@ var vm = new Vue({
119119
message: '123'
120120
}
121121
})
122-
vm.message = 'new message' // change data
122+
vm.message = 'new message' // データを変更する
123123
vm.$el.textContent === 'new message' // false
124124
Vue.nextTick(function() {
125125
vm.$el.textContent === 'new message' // true
126126
})
127127
```
128128

129-
There is also the `vm.$nextTick()` instance method, which is especially handy inside components, because it doesn't need global `Vue` and its callback's `this` context will be automatically bound to the current component instance:
129+
`vm.$nextTick()` というインスタンスメソッドもあります。これは、グローバルな Vue を必要とせず、コールバックの `this` コンテキストが自動的に現在の Vue インスタンスに束縛されるため、コンポーネント内で特に役立ちます:
130130

131131
```js
132132
Vue.component('example', {
@@ -148,7 +148,7 @@ Vue.component('example', {
148148
})
149149
```
150150

151-
Since `$nextTick()` returns a promise, you can achieve the same as the above using the new [ES2017 async/await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) syntax:
151+
`$nextTick()` は Promise を返却するため、新しい [ES2017 async/await](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/async_function) 構文を用いて、同じことができます:
152152

153153
```js
154154
methods: {

0 commit comments

Comments
 (0)