1
- # Computed and watch
1
+ # computed と watch
2
2
3
- > This section uses [ single-file component ] ( ../guide/single-file-component.html ) syntax for code examples
3
+ > このセクションでは、コード例に [ 単一ファイルコンポーネント ] ( ../guide/single-file-component.html ) 構文を使用します
4
4
5
5
## ` computed `
6
6
7
- Takes a getter function and returns an immutable reactive [ ref] ( ./refs-api.html#ref ) object for the returned value from the getter.
7
+ ゲッタ関数を受け取り、ゲッタからの戻り値に対してイミュータブルでリアクティブな [ ref] ( ./refs-api.html#ref ) オブジェクトを返します。
8
8
9
9
``` js
10
10
const count = ref (1 )
11
11
const plusOne = computed (() => count .value + 1 )
12
12
13
13
console .log (plusOne .value ) // 2
14
14
15
- plusOne .value ++ // error
15
+ plusOne .value ++ // エラー
16
16
```
17
17
18
- Alternatively, it can take an object with ` get ` and ` set ` functions to create a writable ref object.
18
+ または、 ` get ` と ` set ` 関数のオブジェクトを受け取り、書き込み可能な ref オブジェクトを作成することもできます。
19
19
20
20
``` js
21
21
const count = ref (1 )
@@ -30,16 +30,16 @@ plusOne.value = 1
30
30
console .log (count .value ) // 0
31
31
```
32
32
33
- ** Typing :**
33
+ ** 型 :**
34
34
35
35
``` ts
36
- // read-only
36
+ // 読み取り専用
37
37
function computed<T >(
38
38
getter : () => T ,
39
39
debuggerOptions ? : DebuggerOptions
40
40
): Readonly <Ref <Readonly <T >>>
41
41
42
- // writable
42
+ // 書き込み可能
43
43
function computed<T >(
44
44
options : {
45
45
get: () => T
@@ -63,21 +63,21 @@ interface DebuggerEvent {
63
63
64
64
## ` watchEffect `
65
65
66
- Runs a function immediately while reactively tracking its dependencies and re-runs it whenever the dependencies are changed.
66
+ 依存関係をリアクティブに追跡しながら関数を即時実行し、依存関係が変更されるたびに関数を再実行します。
67
67
68
68
``` js
69
69
const count = ref (0 )
70
70
71
71
watchEffect (() => console .log (count .value ))
72
- // -> logs 0
72
+ // -> 0 がログに出力される
73
73
74
74
setTimeout (() => {
75
75
count .value ++
76
- // -> logs 1
76
+ // -> 1 がログに出力される
77
77
}, 100 )
78
78
```
79
79
80
- ** Typing :**
80
+ ** 型 :**
81
81
82
82
``` ts
83
83
function watchEffect(
@@ -86,7 +86,7 @@ function watchEffect(
86
86
): StopHandle
87
87
88
88
interface WatchEffectOptions {
89
- flush? : ' pre' | ' post' | ' sync' // default : 'pre'
89
+ flush? : ' pre' | ' post' | ' sync' // デフォルト : 'pre'
90
90
onTrack? : (event : DebuggerEvent ) => void
91
91
onTrigger? : (event : DebuggerEvent ) => void
92
92
}
@@ -103,32 +103,32 @@ type InvalidateCbRegistrator = (invalidate: () => void) => void
103
103
type StopHandle = () => void
104
104
` ` `
105
105
106
- **See also **: [ ` watchEffect ` guide ](../guide/reactivity-computed-watchers.html#watcheffect)
106
+ **参照 **: [ ` watchEffect ` ガイド ](../guide/reactivity-computed-watchers.html#watcheffect)
107
107
108
108
## ` watchPostEffect ` <Badge text="3.2+" />
109
109
110
- Alias of ` watchEffect ` with ` flush : ' post' ` option.
110
+ ` flush : ' post' ` オプションがついた ` watchEffect ` のエイリアスです。
111
111
112
112
## ` watchSyncEffect ` <Badge text="3.2+" />
113
113
114
- Alias of ` watchEffect ` with ` flush : ' sync' ` option.
114
+ ` flush : ' sync' ` オプションがついた ` watchEffect ` のエイリアスです。
115
115
116
116
## ` watch `
117
117
118
- The ` watch ` API is the exact equivalent of the Options API [this.\$ watch](./instance-methods.html#watch) (and the corresponding [watch](./options-data.html#watch) option). ` watch ` requires watching a specific data source and applies side effects in a separate callback function. It also is lazy by default - i.e. the callback is only called when the watched source has changed.
118
+ ` watch ` API は Options API の [this.\$ watch](./instance-methods.html#watch)(および対応する [watch](./options-data.html#watch) オプション)とまったく同等です。 ` watch ` は特定のデータソースを監視する必要があり、別のコールバック関数で副作用を適用します。また、デフォルトでは遅延処理となります。つまり、監視対象のソースが変更されたときにのみコールバックが呼び出されます。
119
119
120
- - Compared to [watchEffect](#watcheffect), ` watch ` allows us to :
120
+ - [watchEffect](#watcheffect) と比較すると、 ` watch ` では以下のことが可能です :
121
121
122
- - Perform the side effect lazily;
123
- - Be more specific about what state should trigger the watcher to re-run;
124
- - Access both the previous and current value of the watched state.
122
+ - 副作用を遅延実行できる。
123
+ - どの状態がウォッチャの再実行をトリガすべきか、より具体的に指定できる。
124
+ - 監視している状態の、以前の値と現在の値の両方にアクセスできる。
125
125
126
- ### Watching a Single Source
126
+ ### 単一のソースを監視する
127
127
128
- A watcher data source can either be a getter function that returns a value, or directly a [ref](./refs-api.html#ref):
128
+ ウォッチャのデータソースは、値を返すゲッタ関数か、直接 [ref](./refs-api.html#ref) を指定できます :
129
129
130
130
` ` ` js
131
- // watching a getter
131
+ // ゲッタを監視
132
132
const state = reactive ({ count: 0 })
133
133
watch (
134
134
() => state .count ,
@@ -137,31 +137,31 @@ watch(
137
137
}
138
138
)
139
139
140
- // directly watching a ref
140
+ // ref を直接監視
141
141
const count = ref (0 )
142
142
watch (count , (count , prevCount ) => {
143
143
/* ... */
144
144
})
145
145
```
146
146
147
- ### Watching Multiple Sources
147
+ ### 複数のソースを監視する
148
148
149
- A watcher can also watch multiple sources at the same time using an array :
149
+ ウォッチャは配列を使って複数のソースを同時に監視することもできます :
150
150
151
151
``` js
152
152
watch ([fooRef, barRef], ([foo , bar ], [prevFoo , prevBar ]) => {
153
153
/* ... */
154
154
})
155
155
```
156
156
157
- ### Shared Behavior with ` watchEffect `
157
+ ### ` watchEffect ` との共有動作
158
158
159
- ` watch ` shares behavior with [ ` watchEffect ` ] ( #watcheffect ) in terms of [ manual stoppage ] ( ../guide/reactivity-computed-watchers.html#stopping-the-watcher ) , [ side effect invalidation ] ( ../guide/reactivity-computed-watchers.html#side-effect-invalidation ) (with ` onInvalidate ` passed to the callback as the 3rd argument instead), [ flush timing ] ( ../guide/reactivity-computed-watchers.html#effect-flush-timing ) and [ debugging ] ( ../guide/reactivity-computed-watchers.html#watcher-debugging ) .
159
+ ` watch ` は [ 手動停止 ] ( ../guide/reactivity-computed-watchers.html#監視の停止 ) 、 [ 副作用の無効化 ] ( ../guide/reactivity-computed-watchers.html#副作用の無効化 ) ( ` onInvalidate ` を第 3 引数としてコールバックに渡す)、 [ フラッシュのタイミング ] ( ../guide/reactivity-computed-watchers.html#作用フラッシュのタイミング ) 、 [ デバッグ ] ( ../guide/reactivity-computed-watchers.html#watcher-のデバッグ ) に関して、 [ ` watchEffect ` ] ( #watcheffect ) と動作を共有しています。
160
160
161
- ** Typing :**
161
+ ** 型 :**
162
162
163
163
``` ts
164
- // watching single source
164
+ // 単一のソースを監視する
165
165
function watch<T >(
166
166
source : WatcherSource <T >,
167
167
callback : (
@@ -172,7 +172,7 @@ function watch<T>(
172
172
options ? : WatchOptions
173
173
): StopHandle
174
174
175
- // watching multiple sources
175
+ // 複数のソースを監視する
176
176
function watch<T extends WatcherSource <unknown >[]>(
177
177
sources : T
178
178
callback : (
@@ -189,11 +189,11 @@ type MapSources<T> = {
189
189
[K in keyof T ]: T [K ] extends WatcherSource <infer V > ? V : never
190
190
}
191
191
192
- // see `watchEffect` typing for shared options
192
+ // 共有オプションについては `watchEffect` の型を参照
193
193
interface WatchOptions extends WatchEffectOptions {
194
- immediate? : boolean // default : false
194
+ immediate? : boolean // デフォルト : false
195
195
deep? : boolean
196
196
}
197
197
```
198
198
199
- ** See also ** : [ ` watch ` guide ] ( ../guide/reactivity-computed-watchers.html#watch )
199
+ ** 参照 ** : [ ` watch ` ガイド ] ( ../guide/reactivity-computed-watchers.html#watch )
0 commit comments