Skip to content

Commit 8ec48dd

Browse files
committed
docs: translate api > sfc script setup
1 parent 6a427aa commit 8ec48dd

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

src/api/sfc-script-setup.md

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@ sidebarDepth: 1
44

55
# SFC `<script setup>`
66

7-
`<script setup>` is a compile-time syntactic sugar for using [Composition API](/api/composition-api.html) inside Single File Components (SFCs). It is the recommended syntax if you are using both SFCs and Composition API. It provides a number of advantages over the normal `<script>` syntax:
7+
`<script setup>` は単一ファイルコンポーネント(SFC)内で [Composition API](/api/composition-api.html) を使用するコンパイル時のシンタックスシュガー(糖衣構文)です。SFC と Composition API の両方を使うならば、おすすめの構文です。これは通常の `<script>` 構文よりも、多くの利点があります:
88

9-
- More succinct code with less boilerplate
10-
- Ability to declare props and emitted events using pure TypeScript
11-
- Better runtime performance (the template is compiled into a render function in the same scope, without an intermediate proxy)
12-
- Better IDE type-inference performance (less work for the language server to extract types from code)
9+
- ボイラープレートが少なくて、より簡潔なコード
10+
- 純粋な TypeScript を使ってプロパティと発行されたイベントを宣言する機能
11+
- 実行時のパフォーマンスの向上(テンプレートは中間プロキシなしに同じスコープ内のレンダリング関数にコンパイルされます)
12+
- IDE で型推論のパフォーマンス向上(言語サーバがコードから型を抽出する作業が減ります)
1313

14-
## Basic Syntax
14+
## 基本の構文
1515

16-
To opt-in to the syntax, add the `setup` attribute to the `<script>` block:
16+
この構文を導入するには、`setup` 属性を `<script>` ブロックに追加します:
1717

1818
```vue
1919
<script setup>
2020
console.log('hello script setup')
2121
</script>
2222
```
2323

24-
The code inside is compiled as the content of the component's `setup()` function. This means that unlike normal `<script>`, which only executes once when the component is first imported, code inside `<script setup>` will **execute every time an instance of the component is created**.
24+
内部のコードは、コンポーネントの `setup()` 関数の内容としてコンパイルされます。これはつまり、通常の `<script>` とは違って、コンポーネントが最初にインポートされたときに一度だけ実行されるのではなく、`<script setup>` 内のコードは **コンポーネントのインスタンスが作成されるたびに実行される** ということです。
2525

26-
### Top-level bindings are exposed to template
26+
### トップレベルのバインディングはテンプレートに公開
2727

28-
When using `<script setup>`, any top-level bindings (including variables, function declarations, and imports) declared inside `<script setup>` are directly usable in the template:
28+
`<script setup>` を使用する場合、`<script setup>` 内で宣言されたトップレベルのバインディング(変数、関数宣言、インポートを含む)は、テンプレートで直接使用できます:
2929

3030
```vue
3131
<script setup>
32-
// variable
32+
// 変数
3333
const msg = 'Hello!'
3434
35-
// functions
35+
// 関数
3636
function log() {
3737
console.log(msg)
3838
}
@@ -43,7 +43,7 @@ function log() {
4343
</template>
4444
```
4545

46-
Imports are exposed in the same fashion. This means you can directly use an imported helper function in template expressions without having to expose it via the `methods` option:
46+
インポートも同じように公開されます。これはつまり、インポートされたヘルパー関数を `methods` オプションで公開することなくテンプレート式で直接使用できます:
4747

4848
```vue
4949
<script setup>
@@ -55,9 +55,9 @@ import { capitalize } from './helpers'
5555
</template>
5656
```
5757

58-
## Reactivity
58+
## リアクティビティ
5959

60-
Reactive state needs to be explicitly created using [Reactivity APIs](/api/basic-reactivity.html). Similar to values returned from a `setup()` function, refs are automatically unwrapped when referenced in templates:
60+
リアクティブな状態は [リアクティビティ API](/api/basic-reactivity.html) を使って明示的に作成する必要があります。`setup()` 関数から返された値と同じように、テンプレート内で参照されるときに ref は自動的にアンラップされます:
6161

6262
```vue
6363
<script setup>
@@ -71,9 +71,9 @@ const count = ref(0)
7171
</template>
7272
```
7373

74-
## Using Components
74+
## コンポーネントの使用
7575

76-
Values in the scope of `<script setup>` can also be used directly as custom component tag names:
76+
`<script setup>` のスコープ内の値は、カスタムコンポーネントのタグ名としても直接使用できます:
7777

7878
```vue
7979
<script setup>
@@ -85,11 +85,11 @@ import MyComponent from './MyComponent.vue'
8585
</template>
8686
```
8787

88-
Think of `MyComponent` as being referenced as a variable. If you have used JSX, the mental model is similar here. The kebab-case equivalent `<my-component>` also works in the template - however PascalCase component tags are strongly recommended for consistency. It also helps differentiating from native custom elements.
88+
`MyComponent` を変数として参照していると考えてください。JSX を使ったことがあれば、このメンタルモデルは似ています。ケバブケースの `<my-component>` も同じようにテンプレートで動作します。しかし、一貫性を保つために、パスカルケースのコンポーネントタグを強く推奨します。これはネイティブのカスタム要素と区別するのにも役立ちます。
8989

90-
### Dynamic Components
90+
### 動的コンポーネント
9191

92-
Since components are referenced as variables instead of registered under string keys, you should use dynamic `:is` binding when using dynamic components inside `<script setup>`:
92+
コンポーネントは、文字列キーで登録されるのではなく変数として参照されるため、`<script setup>` 内で動的コンポーネントを使う場合は、動的な `:is` バインディングを使う必要があります:
9393

9494
```vue
9595
<script setup>
@@ -103,21 +103,21 @@ import Bar from './Bar.vue'
103103
</template>
104104
```
105105

106-
Note how the components can be used as variables in a ternary expression.
106+
三項演算子で変数としてコンポーネントをどのように使うことができるかに注意してください。
107107

108-
### Recursive Components
108+
### 再帰的コンポーネント
109109

110-
An SFC can implicitly refer to itself via its filename. E.g. a file named `FooBar.vue` can refer to itself as `<FooBar/>` in its template.
110+
SFC はそのファイル名を介して、暗黙的に自身を参照できます。例えば、`FooBar.vue` というファイル名は、そのテンプレート内で `<FooBar/>` として自身を参照できます。
111111

112-
Note this has lower priority than imported components. If you have a named import that conflicts with the component's inferred name, you can alias the import:
112+
これはインポートされたコンポーネントよりも優先度が低いことに注意してください。コンポーネントの推論された名前と競合する名前付きインポートがある場合、インポートでエイリアスを作成できます:
113113

114114
```js
115115
import { FooBar as FooBarChild } from './components'
116116
```
117117

118-
### Namespaced Components
118+
### 名前空間付きコンポーネント
119119

120-
You can use component tags with dots like `<Foo.Bar>` to refer to components nested under object properties. This is useful when you import multiple components from a single file:
120+
`<Foo.Bar>` のようにドット付きのコンポーネントタグを使って、オブジェクトプロパティの下にネストしたコンポーネントを参照することができます。これは単一のファイルから複数のコンポーネントをインポートするときに便利です:
121121

122122
```vue
123123
<script setup>
@@ -131,32 +131,33 @@ import * as Form from './form-components'
131131
</template>
132132
```
133133

134-
## Using Custom Directives
134+
## カスタムディレクティブの使用
135135

136-
Globally registered custom directives just work as expected, and local ones can be used directly in the template, much like we explained above for components.
136+
グローバルに登録されたカスタムディレクティブは期待通りに動作して、ローカルのカスタムディレクティブは前述のコンポーネントで説明したようにテンプレートで直接使用できます。
137137

138-
But there's one restriction to be aware of: You must name local custom directives according to the following schema: `vNameOfDirective` in order for them to be directly usable in the template.
138+
しかし、注意する制限が 1 つあります: ローカルのカスタムディレクティブ名は、以下のスキーマに従わなければなりません: `vNameOfDirective` はテンプレート内で直接使用できるようにするためのものです。
139139

140140
```html
141141
<script setup>
142142
const vMyDirective = {
143143
beforeMount: (el) => {
144-
// do something with the element
144+
// 要素でなにかをします
145145
}
146146
}
147147
</script>
148148
<template>
149149
<h1 v-my-directive>This is a Heading</h1>
150150
</template>
151151
```
152+
152153
```html
153154
<script setup>
154-
// imports also work, and can be renamed to fit the required naming schema
155+
// インポートも可能で、必要な命名スキーマに合わせてリネームすることができます
155156
import { myDirective as vMyDirective } from './MyDirective.js'
156157
</script>
157158
```
158159

159-
## `defineProps` and `defineEmits`
160+
## `defineProps` `defineEmits`
160161

161162
To declare `props` and `emits` in `<script setup>`, you must use the `defineProps` and `defineEmits` APIs, which provide full type inference support and are automatically available inside `<script setup>`:
162163

@@ -203,7 +204,7 @@ defineExpose({
203204

204205
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).
205206

206-
## `useSlots` and `useAttrs`
207+
## `useSlots` `useAttrs`
207208

208209
Usage of `slots` and `attrs` inside `<script setup>` should be relatively rare, since you can access them directly as `$slots` and `$attrs` in the template. In the rare case where you do need them, use the `useSlots` and `useAttrs` helpers respectively:
209210

@@ -218,7 +219,7 @@ const attrs = useAttrs()
218219

219220
`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.
220221

221-
## Usage alongside normal `<script>`
222+
## 通常の `<script>` との併用
222223

223224
`<script setup>` can be used alongside normal `<script>`. A normal `<script>` may be needed in cases where you need to:
224225

@@ -243,7 +244,7 @@ export default {
243244
</script>
244245
```
245246

246-
## Top-level `await`
247+
## トップレベルの `await`
247248

248249
Top-level `await` can be used inside `<script setup>`. The resulting code will be compiled as `async setup()`:
249250

@@ -259,9 +260,9 @@ In addition, the awaited expression will be automatically compiled in a format t
259260
`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.
260261
:::
261262

262-
## TypeScript-only Features
263+
## TypeScript のみの機能
263264

264-
### Type-only props/emit declarations
265+
### 型のみの props/emit 宣言
265266

266267
Props and emits can also be declared using pure-type syntax by passing a literal type argument to `defineProps` or `defineEmits`:
267268

@@ -294,7 +295,7 @@ const emit = defineEmits<{
294295

295296
Currently complex types and type imports from other files are not supported. It is theoretically possible to support type imports in the future.
296297

297-
### Default props values when using type declaration
298+
### 型宣言を使用時のデフォルトの props
298299

299300
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:
300301

@@ -312,6 +313,6 @@ const props = withDefaults(defineProps<Props>(), {
312313

313314
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.
314315

315-
## Restriction: No Src Imports
316+
## 制限: src インポートの禁止
316317

317318
Due to the difference in module execution semantics, code inside `<script setup>` relies on the context of an SFC. When moved into external `.js` or `.ts` files, it may lead to confusion for both developers and tools. Therefore, **`<script setup>`** cannot be used with the `src` attribute.

0 commit comments

Comments
 (0)