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
+48-48Lines changed: 48 additions & 48 deletions
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,9 @@ sidebarDepth: 1
15
15
16
16
To opt-in to the syntax, add the `setup` attribute to the `<script>` block:
17
17
18
-
```html
18
+
```vue
19
19
<script setup>
20
-
console.log('hello script setup')
20
+
console.log('hello script setup')
21
21
</script>
22
22
```
23
23
@@ -27,15 +27,15 @@ The code inside are compiled as the content of the component's `setup()` functio
27
27
28
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:
29
29
30
-
```html
30
+
```vue
31
31
<script setup>
32
-
// variable
33
-
constmsg='Hello!'
32
+
// variable
33
+
const msg = 'Hello!'
34
34
35
-
// functions
36
-
functionlog() {
37
-
console.log(msg)
38
-
}
35
+
// functions
36
+
function log() {
37
+
console.log(msg)
38
+
}
39
39
</script>
40
40
41
41
<template>
@@ -45,9 +45,9 @@ When using `<script setup>`, any top-level bindings (including variables, functi
45
45
46
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:
47
47
48
-
```html
48
+
```vue
49
49
<script setup>
50
-
import { capitalize } from'./helpers'
50
+
import { capitalize } from './helpers'
51
51
</script>
52
52
53
53
<template>
@@ -59,11 +59,11 @@ Imports are exposed in the same fashion. This means you can directly use an impo
59
59
60
60
Reactive state needs to be explicitly created using [Reactivity APIs](/api/basic-reactivity.html). Similar to returned values from the `setup()` functions, refs are automatically unwrapped when referenced in templates:
61
61
62
-
```html
62
+
```vue
63
63
<script setup>
64
-
import { ref } from'vue'
64
+
import { ref } from 'vue'
65
65
66
-
constcount=ref(0)
66
+
const count = ref(0)
67
67
</script>
68
68
69
69
<template>
@@ -75,9 +75,9 @@ Reactive state needs to be explicitly created using [Reactivity APIs](/api/basic
75
75
76
76
Values in the scope of `<script setup>` can also be used directly as custom component tag names:
77
77
78
-
```html
78
+
```vue
79
79
<script setup>
80
-
importMyComponentfrom'./MyComponent.vue'
80
+
import MyComponent from './MyComponent.vue'
81
81
</script>
82
82
83
83
<template>
@@ -91,10 +91,10 @@ Think of `MyComponent` as being referenced as a variable. If you have used JSX b
91
91
92
92
Since components are referenced as variables instead of registered under string keys, we should use dynamic `:is` binding when using dynamic components inside `<script setup>`:
93
93
94
-
```html
94
+
```vue
95
95
<script setup>
96
-
importFoofrom'./Foo.vue'
97
-
importBarfrom'./Bar.vue'
96
+
import Foo from './Foo.vue'
97
+
import Bar from './Bar.vue'
98
98
</script>
99
99
100
100
<template>
@@ -119,14 +119,14 @@ import { FooBar as FooBarChild } from './components'
119
119
120
120
To declare options like `props` and `emits` with full type inference support, we can use the `defineProps` and `defineEmits` APIs, which are automatically available inside `<script setup>`:
121
121
122
-
```html
122
+
```vue
123
123
<script setup>
124
-
constprops=defineProps({
125
-
foo:String
126
-
})
124
+
const props = defineProps({
125
+
foo: String
126
+
})
127
127
128
-
constemit=defineEmits(['change', 'delete'])
129
-
// setup code
128
+
const emit = defineEmits(['change', 'delete'])
129
+
// setup code
130
130
</script>
131
131
```
132
132
@@ -146,15 +146,15 @@ Components using `<script setup>` are **closed by default** - i.e. the public in
146
146
147
147
To explicitly expose properties in a `<script setup>` component, use the `defineExpose` compiler macro:
148
148
149
-
```html
149
+
```vue
150
150
<script setup>
151
-
consta=1
152
-
constb=ref(2)
151
+
const a = 1
152
+
const b = ref(2)
153
153
154
-
defineExpose({
155
-
a,
156
-
b
157
-
})
154
+
defineExpose({
155
+
a,
156
+
b
157
+
})
158
158
</script>
159
159
```
160
160
@@ -164,12 +164,12 @@ When a parent gets an instance of this component via template refs, the retrieve
164
164
165
165
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:
166
166
167
-
```html
167
+
```vue
168
168
<script setup>
169
-
import { useSlots, useAttrs } from'vue'
169
+
import { useSlots, useAttrs } from 'vue'
170
170
171
-
constslots=useSlots()
172
-
constattrs=useAttrs()
171
+
const slots = useSlots()
172
+
const attrs = useAttrs()
173
173
</script>
174
174
```
175
175
@@ -183,30 +183,30 @@ Usage of `slots` and `attrs` inside `<script setup>` should be relatively rare,
183
183
- Declaring named exports.
184
184
- Run side effects or create objects that should only execute once.
185
185
186
-
```html
186
+
```vue
187
187
<script>
188
-
// normal <script>, executed in module scope (only once)
189
-
runSideEffectOnce()
190
-
191
-
// declare additional options
192
-
exportdefault {
193
-
inheritAttrs:false,
194
-
customOptions: {}
195
-
}
188
+
// normal <script>, executed in module scope (only once)
189
+
runSideEffectOnce()
190
+
191
+
// declare additional options
192
+
export default {
193
+
inheritAttrs: false,
194
+
customOptions: {}
195
+
}
196
196
</script>
197
197
198
198
<script setup>
199
-
// executed in setup() scope (for each instance)
199
+
// executed in setup() scope (for each instance)
200
200
</script>
201
201
```
202
202
203
203
## Top level await
204
204
205
205
Top level `await` can be used inside `<script setup>`. The resulting code will be compiled as `async setup()`:
0 commit comments