You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/api/sfc-script-setup.md
+40-39Lines changed: 40 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -4,35 +4,35 @@ sidebarDepth: 1
4
4
5
5
# SFC `<script setup>`
6
6
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>`構文よりも、多くの利点があります:
8
8
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)
To opt-in to the syntax, add the `setup`attribute to the `<script>`block:
16
+
この構文を導入するには、`setup`属性を `<script>`ブロックに追加します:
17
17
18
18
```vue
19
19
<script setup>
20
20
console.log('hello script setup')
21
21
</script>
22
22
```
23
23
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**.
When using `<script setup>`, any top-level bindings (including variables, function declarations, and imports) declared inside `<script setup>`are directly usable in the template:
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:
@@ -55,9 +55,9 @@ import { capitalize } from './helpers'
55
55
</template>
56
56
```
57
57
58
-
## Reactivity
58
+
## リアクティビティ
59
59
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:
@@ -85,11 +85,11 @@ import MyComponent from './MyComponent.vue'
85
85
</template>
86
86
```
87
87
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.
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>`:
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:
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:
@@ -131,32 +131,33 @@ import * as Form from './form-components'
131
131
</template>
132
132
```
133
133
134
-
## Using Custom Directives
134
+
## カスタムディレクティブの使用
135
135
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.
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.
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>`:
162
163
@@ -203,7 +204,7 @@ defineExpose({
203
204
204
205
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).
205
206
206
-
## `useSlots`and`useAttrs`
207
+
## `useSlots`と`useAttrs`
207
208
208
209
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:
209
210
@@ -218,7 +219,7 @@ const attrs = useAttrs()
218
219
219
220
`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.
220
221
221
-
## Usage alongside normal `<script>`
222
+
## 通常の `<script>` との併用
222
223
223
224
`<script setup>` can be used alongside normal `<script>`. A normal `<script>` may be needed in cases where you need to:
224
225
@@ -243,7 +244,7 @@ export default {
243
244
</script>
244
245
```
245
246
246
-
## Top-level`await`
247
+
## トップレベルの`await`
247
248
248
249
Top-level `await` can be used inside `<script setup>`. The resulting code will be compiled as `async setup()`:
249
250
@@ -259,9 +260,9 @@ In addition, the awaited expression will be automatically compiled in a format t
259
260
`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.
260
261
:::
261
262
262
-
## TypeScript-only Features
263
+
## TypeScript のみの機能
263
264
264
-
### Type-only props/emit declarations
265
+
### 型のみの props/emit 宣言
265
266
266
267
Props and emits can also be declared using pure-type syntax by passing a literal type argument to `defineProps` or `defineEmits`:
267
268
@@ -294,7 +295,7 @@ const emit = defineEmits<{
294
295
295
296
Currently complex types and type imports from other files are not supported. It is theoretically possible to support type imports in the future.
296
297
297
-
### Default props values when using type declaration
298
+
### 型宣言を使用時のデフォルトの props 値
298
299
299
300
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:
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.
314
315
315
-
## Restriction: No Src Imports
316
+
## 制限: src インポートの禁止
316
317
317
318
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