@@ -95,8 +95,7 @@ watchEffect(async (onInvalidate) => {
95
95
96
96
Vue のリアクティブシステムは、無効になった変更をバッファリングし、非同期に処理することによって、おなじ "tick" の中での複数の状態の変化に対して、不要な重複の呼び出しを避けることができています。内部的には、コンポーネントの ` update ` 関数も、監視されている作用の 1 つです。ユーザーによる作用がキューに入っている場合、デフォルトではすべてのコンポーネントの更新の ** 前に** 呼び出されます:
97
97
98
- ``` html
99
-
98
+ ``` vue
100
99
<template>
101
100
<div>{{ count }}</div>
102
101
</template>
@@ -201,15 +200,15 @@ watch(count, (count, prevCount) => {
201
200
ウォッチャは、配列を利用することで、複数のデータソースを同時に監視できます:
202
201
203
202
``` js
204
- const firstName = ref (' ' );
205
- const lastName = ref (' ' );
203
+ const firstName = ref (' ' )
204
+ const lastName = ref (' ' )
206
205
207
206
watch ([firstName, lastName], (newValues , prevValues ) => {
208
- console .log (newValues, prevValues);
207
+ console .log (newValues, prevValues)
209
208
})
210
209
211
- firstName .value = " John" ; // logs: ["John",""] ["", ""]
212
- lastName .value = " Smith" ; // logs: ["John", "Smith"] ["John", ""]
210
+ firstName .value = ' John' // logs: ["John", ""] ["", ""]
211
+ lastName .value = ' Smith' // logs: ["John", "Smith"] ["John", ""]
213
212
```
214
213
215
214
### リアクティブなオブジェクトの監視
@@ -222,8 +221,9 @@ const numbers = reactive([1, 2, 3, 4])
222
221
watch (
223
222
() => [... numbers],
224
223
(numbers , prevNumbers ) => {
225
- console .log (numbers, prevNumbers);
226
- })
224
+ console .log (numbers, prevNumbers)
225
+ }
226
+ )
227
227
228
228
numbers .push (5 ) // logs: [1,2,3,4,5] [1,2,3,4]
229
229
```
@@ -234,59 +234,59 @@ numbers.push(5) // logs: [1,2,3,4,5] [1,2,3,4]
234
234
const state = reactive ({
235
235
id: 1 ,
236
236
attributes: {
237
- name: " " ,
238
- },
239
- });
237
+ name: ' '
238
+ }
239
+ })
240
240
241
241
watch (
242
242
() => state,
243
243
(state , prevState ) => {
244
244
console .log (
245
- " not deep " ,
245
+ ' not deep' ,
246
246
state .attributes .name ,
247
247
prevState .attributes .name
248
- );
248
+ )
249
249
}
250
- );
250
+ )
251
251
252
252
watch (
253
253
() => state,
254
254
(state , prevState ) => {
255
255
console .log (
256
- " deep " ,
256
+ ' deep' ,
257
257
state .attributes .name ,
258
258
prevState .attributes .name
259
- );
259
+ )
260
260
},
261
261
{ deep: true }
262
- );
262
+ )
263
263
264
- state .attributes .name = " Alex" ; // Logs: "deep " "Alex" "Alex"
264
+ state .attributes .name = ' Alex' // Logs: "deep" "Alex" "Alex"
265
265
```
266
266
267
267
しかし、リアクティブなオブジェクトや配列を監視すると、そのオブジェクトの状態の現在値と前回値の両方について参照が常に返されます。深くネストされたオブジェクトや配列を完全に監視するためには、値のディープコピーが必要な場合があります。これは [ lodash.cloneDeep] ( https://lodash.com/docs/4.17.15#cloneDeep ) のようなユーティリティで実現できます。
268
268
269
269
``` js
270
- import _ from ' lodash' ;
270
+ import _ from ' lodash'
271
271
272
272
const state = reactive ({
273
273
id: 1 ,
274
274
attributes: {
275
- name: " " ,
276
- },
277
- });
275
+ name: ' '
276
+ }
277
+ })
278
278
279
279
watch (
280
280
() => _ .cloneDeep (state),
281
281
(state , prevState ) => {
282
282
console .log (
283
283
state .attributes .name ,
284
284
prevState .attributes .name
285
- );
285
+ )
286
286
}
287
- );
287
+ )
288
288
289
- state .attributes .name = " Alex" ; // Logs: "Alex" ""
289
+ state .attributes .name = ' Alex' ; // Logs: "Alex" ""
290
290
```
291
291
292
292
### ` watchEffect ` との振る舞いの共有
0 commit comments