Skip to content

Commit 55505e7

Browse files
committed
use vue codeblock
1 parent d337d21 commit 55505e7

File tree

1 file changed

+48
-48
lines changed

1 file changed

+48
-48
lines changed

src/api/sfc-script-setup.md

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ sidebarDepth: 1
1515

1616
To opt-in to the syntax, add the `setup` attribute to the `<script>` block:
1717

18-
```html
18+
```vue
1919
<script setup>
20-
console.log('hello script setup')
20+
console.log('hello script setup')
2121
</script>
2222
```
2323

@@ -27,15 +27,15 @@ The code inside are compiled as the content of the component's `setup()` functio
2727

2828
When using `<script setup>`, any top-level bindings (including variables, function declarations, and imports) declared inside `<script setup>` are directly usable in the template:
2929

30-
```html
30+
```vue
3131
<script setup>
32-
// variable
33-
const msg = 'Hello!'
32+
// variable
33+
const msg = 'Hello!'
3434
35-
// functions
36-
function log() {
37-
console.log(msg)
38-
}
35+
// functions
36+
function log() {
37+
console.log(msg)
38+
}
3939
</script>
4040
4141
<template>
@@ -45,9 +45,9 @@ When using `<script setup>`, any top-level bindings (including variables, functi
4545

4646
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:
4747

48-
```html
48+
```vue
4949
<script setup>
50-
import { capitalize } from './helpers'
50+
import { capitalize } from './helpers'
5151
</script>
5252
5353
<template>
@@ -59,11 +59,11 @@ Imports are exposed in the same fashion. This means you can directly use an impo
5959

6060
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:
6161

62-
```html
62+
```vue
6363
<script setup>
64-
import { ref } from 'vue'
64+
import { ref } from 'vue'
6565
66-
const count = ref(0)
66+
const count = ref(0)
6767
</script>
6868
6969
<template>
@@ -75,9 +75,9 @@ Reactive state needs to be explicitly created using [Reactivity APIs](/api/basic
7575

7676
Values in the scope of `<script setup>` can also be used directly as custom component tag names:
7777

78-
```html
78+
```vue
7979
<script setup>
80-
import MyComponent from './MyComponent.vue'
80+
import MyComponent from './MyComponent.vue'
8181
</script>
8282
8383
<template>
@@ -91,10 +91,10 @@ Think of `MyComponent` as being referenced as a variable. If you have used JSX b
9191

9292
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>`:
9393

94-
```html
94+
```vue
9595
<script setup>
96-
import Foo from './Foo.vue'
97-
import Bar from './Bar.vue'
96+
import Foo from './Foo.vue'
97+
import Bar from './Bar.vue'
9898
</script>
9999
100100
<template>
@@ -119,14 +119,14 @@ import { FooBar as FooBarChild } from './components'
119119

120120
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>`:
121121

122-
```html
122+
```vue
123123
<script setup>
124-
const props = defineProps({
125-
foo: String
126-
})
124+
const props = defineProps({
125+
foo: String
126+
})
127127
128-
const emit = defineEmits(['change', 'delete'])
129-
// setup code
128+
const emit = defineEmits(['change', 'delete'])
129+
// setup code
130130
</script>
131131
```
132132

@@ -146,15 +146,15 @@ Components using `<script setup>` are **closed by default** - i.e. the public in
146146

147147
To explicitly expose properties in a `<script setup>` component, use the `defineExpose` compiler macro:
148148

149-
```html
149+
```vue
150150
<script setup>
151-
const a = 1
152-
const b = ref(2)
151+
const a = 1
152+
const b = ref(2)
153153
154-
defineExpose({
155-
a,
156-
b
157-
})
154+
defineExpose({
155+
a,
156+
b
157+
})
158158
</script>
159159
```
160160

@@ -164,12 +164,12 @@ When a parent gets an instance of this component via template refs, the retrieve
164164

165165
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:
166166

167-
```html
167+
```vue
168168
<script setup>
169-
import { useSlots, useAttrs } from 'vue'
169+
import { useSlots, useAttrs } from 'vue'
170170
171-
const slots = useSlots()
172-
const attrs = useAttrs()
171+
const slots = useSlots()
172+
const attrs = useAttrs()
173173
</script>
174174
```
175175

@@ -183,30 +183,30 @@ Usage of `slots` and `attrs` inside `<script setup>` should be relatively rare,
183183
- Declaring named exports.
184184
- Run side effects or create objects that should only execute once.
185185

186-
```html
186+
```vue
187187
<script>
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-
}
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+
}
196196
</script>
197197
198198
<script setup>
199-
// executed in setup() scope (for each instance)
199+
// executed in setup() scope (for each instance)
200200
</script>
201201
```
202202

203203
## Top level await
204204

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

207-
```html
207+
```vue
208208
<script setup>
209-
const post = await fetch(`/api/post/1`).then(r => r.json())
209+
const post = await fetch(`/api/post/1`).then(r => r.json())
210210
</script>
211211
```
212212

0 commit comments

Comments
 (0)