From 8ec48dd70d341d64a40bf1610b54bc3790616286 Mon Sep 17 00:00:00 2001 From: Naoki Endoh Date: Fri, 26 Nov 2021 21:37:49 +0900 Subject: [PATCH 1/2] docs: translate api > sfc script setup --- src/api/sfc-script-setup.md | 79 +++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/src/api/sfc-script-setup.md b/src/api/sfc-script-setup.md index 4d7d4e55..5df14671 100644 --- a/src/api/sfc-script-setup.md +++ b/src/api/sfc-script-setup.md @@ -4,16 +4,16 @@ sidebarDepth: 1 # SFC ` ``` -The code inside is compiled as the content of the component's `setup()` function. This means that unlike normal ` @@ -149,14 +149,15 @@ const vMyDirective = {

This is a Heading

``` + ```html ``` -## `defineProps` and `defineEmits` +## `defineProps` と `defineEmits` To declare `props` and `emits` in ` ``` -## Top-level `await` +## トップレベルの `await` Top-level `await` can be used inside ` ``` -- `defineProps` and `defineEmits` are **compiler macros** only usable inside ` ``` -When a parent gets an instance of this component via template refs, the retrieved instance will be of the shape `{ a: number, b: number }` (refs are automatically unwrapped just like on normal instances). +親がテンプレート参照を介してこのコンポーネントのインスタンスを取得すると、取得されたインスタンスは `{ a: number, b: number }` という形状になります(ref は通常のインスタンスと同様、自動的にアンラップされます)。 ## `useSlots` と `useAttrs` -Usage of `slots` and `attrs` inside ` ``` -`useSlots` and `useAttrs` are actual runtime functions that return the equivalent of `setupContext.slots` and `setupContext.attrs`. They can be used in normal composition API functions as well. +`useSlots` と `useAttrs` は、`setupContext.slots` と `setupContext.attrs` と同等のものを返す実際のランタイム関数です。これらは通常の Composition API の関数内でも使用できます。 ## 通常の ` ``` :::warning -`render` function is not supported in this scenario. Please use one normal ` ``` -In addition, the awaited expression will be automatically compiled in a format that preserves the current component instance context after the `await`. +さらに、await の対象の式は、`await` 後の現在のコンポーネントのインスタンスのコンテキストを保持する形式で自動的にコンパイルされます。 :::warning Note -`async setup()` must be used in combination with `Suspense`, which is currently still an experimental feature. We plan to finalize and document it in a future release - but if you are curious now, you can refer to its [tests](https://github.com/vuejs/vue-next/blob/master/packages/runtime-core/__tests__/components/Suspense.spec.ts) to see how it works. +`async setup()` は、現在まだ実験的な機能である `Suspense` と組み合わせて使用する必要があります。将来のリリースで完成させてドキュメント化する予定ですが、もし今興味があるのであれば、その[テスト](https://github.com/vuejs/vue-next/blob/master/packages/runtime-core/__tests__/components/Suspense.spec.ts)を参照することで、どのように動作するかを確認できます。 ::: ## TypeScript のみの機能 ### 型のみの props/emit 宣言 -Props and emits can also be declared using pure-type syntax by passing a literal type argument to `defineProps` or `defineEmits`: +`defineProps` や `defineEmits` にリテラル型の引数を渡すことで、純粋な型の構文を使って props や emits を宣言することもできます: ```ts const props = defineProps<{ @@ -281,26 +281,26 @@ const emit = defineEmits<{ }>() ``` -- `defineProps` or `defineEmits` can only use either runtime declaration OR type declaration. Using both at the same time will result in a compile error. +- `defineProps` または `defineEmits` は、実行時の宣言か型宣言のどちらかしか使用できません。両方を同時に使用すると、コンパイルエラーになります。 -- When using type declaration, the equivalent runtime declaration is automatically generated from static analysis to remove the need for double declaration and still ensure correct runtime behavior. +- 型宣言を使用する場合は、同等の実行時宣言が静的解析から自動的に生成されるため、二重の宣言を行う必要がなくなり、実行時の正しい動作が保証されます。 - - In dev mode, the compiler will try to infer corresponding runtime validation from the types. For example here `foo: String` is inferred from the `foo: string` type. If the type is a reference to an imported type, the inferred result will be `foo: null` (equal to `any` type) since the compiler does not have information of external files. + - 開発モードでは、コンパイラは型から対応する実行時バリデーションを推測しようとします。上記の例では `foo: string` という型からは `foo: String` が推測されます。もし型がインポートされた型への参照である場合、コンパイラは外部ファイルの情報を持っていないので、推測される結果は `foo: null`(`any` 型と同じ)になります。 - - In prod mode, the compiler will generate the array format declaration to reduce bundle size (the props here will be compiled into `['foo', 'bar']`) + - プロダクションモードでは、バンドルサイズを小さくするために、コンパイラが配列形式の宣言を生成します(上記の props は `['foo', 'bar']` にコンパイルされます)。 - - The emitted code is still TypeScript with valid typing, which can be further processed by other tools. + - 生成されるコードは有効な型付けがなされた TypeScript であり、他のツールを使ってさらに処理できます。 -- As of now, the type declaration argument must be one of the following to ensure correct static analysis: +- 現在のところ、正しく静的解析を行うためには、型宣言の引数は以下のいずれかでなければなりません: - - A type literal - - A reference to an interface or a type literal in the same file + - 型リテラル + - 同一ファイル内のインタフェースか型リテラルへの参照 - Currently complex types and type imports from other files are not supported. It is theoretically possible to support type imports in the future. + 現在、複雑な型や他のファイルからの型のインポートはサポートされていません。将来的には型のインポートをサポートすることも理論的には可能です。 ### 型宣言を使用時のデフォルトの props 値 -One drawback of the type-only `defineProps` declaration is that it doesn't have a way to provide default values for the props. To resolve this problem, a `withDefaults` compiler macro is also provided: +型のみの `defineProps` 宣言の欠点は、props のデフォルト値を提供する方法がないことです。この問題を解決するために、`withDefaults` コンパイラマクロも用意されています: ```ts interface Props { @@ -314,8 +314,8 @@ const props = withDefaults(defineProps(), { }) ``` -This will be compiled to equivalent runtime props `default` options. In addition, the `withDefaults` helper provides type checks for the default values, and ensures the returned `props` type has the optional flags removed for properties that do have default values declared. +これは、同等な実行時の props の `default` オプションにコンパイルされます。さらに、`withDefaults` ヘルパーは、デフォルト値の型チェックを行います。また、返される `props` の型が、デフォルト値が宣言されているプロパティに対して、省略可能フラグが削除されていることを保証します。 ## 制限: src インポートの禁止 -Due to the difference in module execution semantics, code inside `