13
13
14
14
オプション API を使うときの ` this.$props ` と同様に、` props ` オブジェクトには明示的に宣言されたプロパティのみが含まれます。また、すべての宣言されたプロパティのキーは、親コンポーネントから渡されたかどうかに関わらず ` props ` オブジェクトに存在します。宣言されていない省略可能なプロパティは ` undefined ` という値になります。
15
15
16
- If you need to check the absence of an optional prop, you can give it a Symbol as its default value :
16
+ 省略可能なプロパティが、ないことを確認する必要があるなら、デフォルト値として Symbol を指定できます :
17
17
18
18
``` js
19
19
const isAbsent = Symbol ()
24
24
},
25
25
setup (props ) {
26
26
if (props .foo === isAbsent) {
27
- // foo was not provided.
27
+ // foo は提供されませんでした
28
28
}
29
29
}
30
30
}
48
48
```
49
49
50
50
::: tip
51
- To get type inference for the arguments passed to `setup()`, the use of [defineComponent](global-api.html#definecomponent) is needed.
51
+ ` setup() ` に渡された引数の型推論を得るためには、 [defineComponent ](global - api .html #definecomponent ) を使用する必要があります。
52
52
:::
53
53
54
54
- ** 例**
55
55
56
- With the template :
56
+ テンプレートを使用する場合 :
57
57
58
58
` ` ` vue-html
59
59
<!-- MyBook.vue -->
69
69
const readersNumber = ref(0)
70
70
const book = reactive({ title: 'Vue 3 Guide' })
71
71
72
- // expose to template
72
+ // テンプレートに公開する
73
73
return {
74
74
readersNumber,
75
75
book
79
79
</script>
80
80
` ` `
81
81
82
- With render function :
82
+ Render 関数を使用する場合 :
83
83
84
84
` ` ` js
85
85
// MyBook.vue
90
90
setup() {
91
91
const readersNumber = ref(0)
92
92
const book = reactive({ title: 'Vue 3 Guide' })
93
- // Please note that we need to explicitly use ref value here
93
+ // ここでは明示的に ref 値を使う必要があることに注意してください
94
94
return () => h('div', [readersNumber.value, book.title])
95
95
}
96
96
}
97
97
` ` `
98
98
99
- If you return a render function then you can't return any other properties. If you need to expose properties so that they can be accessed externally, e.g. via a `ref` in the parent, you can use `expose`:
99
+ Render 関数を返す場合は、他のプロパティを返すことはできません。プロパティを公開して、外部からアクセスする必要がある場合、例えば親の ` ref ` を介してなど、 ` expose ` を使用できます :
100
100
101
101
` ` ` js
102
102
// MyBook.vue
106
106
export default {
107
107
setup(props, { expose }) {
108
108
const reset = () => {
109
- // Some reset logic
109
+ // いくつかのリセットロジック
110
110
}
111
111
112
- // Expose can only be called once.
113
- // If you need to expose multiple properties, they must all
114
- // be included in the object passed to expose.
112
+ // expose は一度しか呼べません。
113
+ // 複数のプロパティを公開する必要があるなら、
114
+ // expose に渡されたオブジェクトにすべてのプロパティを含める必要があります。
115
115
expose({
116
116
reset
117
117
})
125
125
126
126
## ライフサイクルフック
127
127
128
- Lifecycle hooks can be registered with directly-imported ` onX ` functions :
128
+ ライフサイクルフックは、直接インポートされた ` onX ` 関数に登録できます :
129
129
130
130
` ` ` js
131
131
import { onMounted, onUpdated, onUnmounted } from 'vue'
@@ -145,9 +145,9 @@ const MyComponent = {
145
145
}
146
146
` ` `
147
147
148
- These lifecycle hook registration functions can only be used synchronously during [ ` setup() ` ] ( #setup ) , since they rely on internal global state to locate the current active instance (the component instance whose ` setup() ` is being called right now). Calling them without a current active instance will result in an error.
148
+ これらのライフサイクルフック登録関数は、 [` setup() ` ](#setup ) の間にのみ、同期的に使用できます。これらの関数は、現在アクティブなインスタンス(今まさに ` setup() ` が呼び出されているコンポーネントインスタンス)を見つけるために内部のグローバル状態に依存しています。現在アクティブなインスタンスがない状態でそれらを呼び出すと、エラーになります。
149
149
150
- The component instance context is also set during the synchronous execution of lifecycle hooks. As a result, watchers and computed properties created synchronously inside of lifecycle hooks are also automatically tore down when the component unmounts.
150
+ コンポーネントのインスタンスコンテキストは、ライフサイクルフックの同期的な実行中にも設定されます。その結果、ライフサイクルフック内で同期的に作成されたウォッチャと算出プロパティも、コンポーネントがアンマウントされるとき自動的に破棄されます。
151
151
152
152
- ** オプション API のライフサイクルオプションと Composition API のマッピング**
153
153
@@ -170,7 +170,7 @@ The component instance context is also set during the synchronous execution of l
170
170
171
171
## Provide / Inject
172
172
173
- ` provide ` and ` inject ` enables dependency injection. Both can only be called during [ ` setup() ` ] ( #setup ) with a current active instance.
173
+ ` provide ` と ` inject ` は依存性の注入を可能にします。 どちらも現在アクティブなインスタンスの [` setup() ` ](#setup ) でのみ呼び出すことができます。
174
174
175
175
- ** 型** :
176
176
@@ -179,31 +179,31 @@ The component instance context is also set during the synchronous execution of l
179
179
180
180
function provide<T>(key: InjectionKey<T> | string, value: T): void
181
181
182
- // without default value
182
+ // デフォルト値なし
183
183
function inject<T>(key: InjectionKey<T> | string): T | undefined
184
- // with default value
184
+ // デフォルト値あり
185
185
function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T
186
- // with factory
186
+ // ファクトリあり
187
187
function inject<T>(
188
188
key: InjectionKey<T> | string,
189
189
defaultValue: () => T,
190
190
treatDefaultAsFactory: true
191
191
): T
192
192
` ` `
193
193
194
- Vue provides an ` InjectionKey ` interface which is a generic type that extends ` Symbol ` . It can be used to sync the type of the injected value between the provider and the consumer :
194
+ Vue は ` Symbol ` を拡張したジェネリック型の ` InjectionKey ` インターフェイスを提供しています。これはプロバイダとコンシューマの間で注入された値の型を同期するために使用できます :
195
195
196
196
` ` ` ts
197
197
import { InjectionKey, provide, inject } from 'vue'
198
198
199
199
const key: InjectionKey<string> = Symbol()
200
200
201
- provide(key, 'foo') // providing non- string value will result in error
201
+ provide(key, 'foo') // string 以外の値を指定するとエラーになります
202
202
203
- const foo = inject(key) // type of foo: string | undefined
203
+ const foo = inject(key) // foo の型 : string | undefined
204
204
` ` `
205
205
206
- If using string keys or non - typed symbols , the type of the injected value will need to be explicitly declared :
206
+ 文字列キーや型指定のない Symbol を使用する場合、注入される値の型を明示的に宣言する必要があります :
207
207
208
208
` ` ` ts
209
209
const foo = inject<string>('foo') // string | undefined
@@ -215,10 +215,10 @@ The component instance context is also set during the synchronous execution of l
215
215
216
216
## ` getCurrentInstance `
217
217
218
- ` getCurrentInstance ` enables access to an internal component instance .
218
+ ` getCurrentInstance ` は内部コンポーネントのインスタンスへのアクセスを可能にします。
219
219
220
220
:::warning
221
- ` getCurrentInstance ` is only exposed for advanced use cases , typically in libraries . Usage of ` getCurrentInstance ` is strongly discouraged in application code . Do ** NOT ** use it as an escape hatch to get the equivalent of ` this ` in Composition API .
221
+ ` getCurrentInstance ` は高度なユースケースのみ、通常はライブラリに公開されます。アプリケーションコードで ` getCurrentInstance ` の使用は強くおすすめしません。 Composition API の ` this ` に相当するものを取得するための避難用ハッチとして ** 使用しない ** でください。
222
222
:::
223
223
224
224
` ` ` ts
@@ -228,31 +228,31 @@ const MyComponent = {
228
228
setup() {
229
229
const internalInstance = getCurrentInstance()
230
230
231
- internalInstance.appContext.config.globalProperties // access to globalProperties
231
+ internalInstance.appContext.config.globalProperties // globalProperties へのアクセス
232
232
}
233
233
}
234
234
` ` `
235
235
236
- ` getCurrentInstance ` ** only ** works during [setup ](#setup ) or [ Lifecycle Hooks ](#lifecycle - hooks )
236
+ ` getCurrentInstance ` は [setup ](#setup ) または [ライフサイクルフック ](#ライフサイクルフック) 中で ** のみ ** 動作します。
237
237
238
- > When using outside of [setup ](#setup ) or [ Lifecycle Hooks ](#lifecycle - hooks ), please call ` getCurrentInstance() ` on ` setup ` and use the instance instead .
238
+ > [setup ](#setup ) や [ライフサイクルフック ](#ライフサイクルフック) の外で使用する場合は、 ` setup ` で ` getCurrentInstance() ` を呼び出し、代わりにインスタンスを使用してください。
239
239
240
240
` ` ` ts
241
241
const MyComponent = {
242
242
setup() {
243
- const internalInstance = getCurrentInstance() // works
243
+ const internalInstance = getCurrentInstance() // 動作します
244
244
245
- const id = useComponentId() // works
245
+ const id = useComponentId() // 動作します
246
246
247
247
const handleClick = () => {
248
- getCurrentInstance() // doesn't work
249
- useComponentId() // doesn't work
248
+ getCurrentInstance() // 動作しません
249
+ useComponentId() // 動作しません
250
250
251
- internalInstance // works
251
+ internalInstance // 動作します
252
252
}
253
253
254
254
onMounted(() => {
255
- getCurrentInstance() // works
255
+ getCurrentInstance() // 動作します
256
256
})
257
257
258
258
return () =>
@@ -266,7 +266,7 @@ const MyComponent = {
266
266
}
267
267
}
268
268
269
- // also works if called on a composable
269
+ // composable で呼び出された場合も動作します
270
270
function useComponentId() {
271
271
return getCurrentInstance().uid
272
272
}
0 commit comments