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: active-rfcs/0040-script-setup.md
+31-33Lines changed: 31 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -63,7 +63,7 @@ export default {
63
63
<scriptsetup>
64
64
// expects props options
65
65
constprops=defineProps({
66
-
foo:String,
66
+
foo:String
67
67
})
68
68
// expects emits options
69
69
constemit=defineEmits(['update', 'delete'])
@@ -150,10 +150,7 @@ import MyComponent from './MyComponent.vue'
150
150
exportdefault {
151
151
setup() {
152
152
returnfunctionrender() {
153
-
return [
154
-
h(Foo),
155
-
h(MyComponent)
156
-
]
153
+
return [h(Foo), h(MyComponent)]
157
154
}
158
155
}
159
156
}
@@ -190,10 +187,7 @@ import Bar from './Bar.vue'
190
187
exportdefault {
191
188
setup() {
192
189
returnfunctionrender() {
193
-
return [
194
-
h(Foo),
195
-
h(someCondition ? Foo : Bar)
196
-
]
190
+
return [h(Foo), h(someCondition ? Foo : Bar)]
197
191
}
198
192
}
199
193
}
@@ -224,9 +218,7 @@ import { directive as vClickOutside } from 'v-click-outside'
224
218
exportdefault {
225
219
setup() {
226
220
returnfunctionrender() {
227
-
returnwithDirectives(h('div'), [
228
-
[vClickOutside]
229
-
])
221
+
returnwithDirectives(h('div'), [[vClickOutside]])
230
222
}
231
223
}
232
224
}
@@ -243,7 +235,7 @@ To declare options like `props` and `emits` with full type inference support, we
243
235
```html
244
236
<scriptsetup>
245
237
constprops=defineProps({
246
-
foo:String,
238
+
foo:String
247
239
})
248
240
249
241
constemit=defineEmits(['change', 'delete'])
@@ -257,7 +249,7 @@ To declare options like `props` and `emits` with full type inference support, we
257
249
```js
258
250
exportdefault {
259
251
props: {
260
-
foo:String,
252
+
foo:String
261
253
},
262
254
emits: ['change', 'delete'],
263
255
setup(props, { emit }) {
@@ -348,16 +340,22 @@ Top level `await` can be used inside `<script setup>`. The resulting `setup()` f
348
340
</script>
349
341
```
350
342
351
-
In addition, the awaited expression will be automatically wrapped with a `withAsyncContext` helper. The helper saves and restores the current instance context so that the current instance context is preserved even after the `await` statement:
343
+
In addition, the awaited expression will be automatically compiled in a format that preserves the current component instance context after the `await`:
352
344
353
345
```js
354
346
import { withAsyncContext } from'vue'
355
347
356
348
exportdefault {
357
349
asyncsetup() {
358
-
constpost=awaitwithAsyncContext(
359
-
fetch(`/api/post/1`).then((r) =>r.json())
360
-
)
350
+
let __temp, __restore
351
+
352
+
constpost=
353
+
(([__temp, __restore] =withAsyncContext(() =>
354
+
fetch(`/api/post/1`).then((r) =>r.json())
355
+
)),
356
+
(__temp =await __temp),
357
+
__restore(),
358
+
__temp)
361
359
362
360
// current instance context preserved
363
361
// e.g. onMounted() will still work.
@@ -379,13 +377,13 @@ To explicitly expose properties in a `<script setup>` component, use the `define
379
377
380
378
```html
381
379
<scriptsetup>
382
-
consta=1
383
-
constb=ref(2)
380
+
consta=1
381
+
constb=ref(2)
384
382
385
-
defineExpose({
386
-
a,
387
-
b
388
-
})
383
+
defineExpose({
384
+
a,
385
+
b
386
+
})
389
387
</script>
390
388
```
391
389
@@ -430,9 +428,9 @@ export default {
430
428
setup() {
431
429
constcount=ref(0)
432
430
return {
433
-
count,
431
+
count
434
432
}
435
-
},
433
+
}
436
434
}
437
435
```
438
436
@@ -469,7 +467,7 @@ If you need to declare these options, use a separate normal `<script>` block wit
469
467
exportdefault {
470
468
name:'CustomName',
471
469
inheritAttrs:false,
472
-
customOptions: {},
470
+
customOptions: {}
473
471
}
474
472
</script>
475
473
@@ -490,7 +488,7 @@ This new scoping model will require tooling adjustments in two aspects:
490
488
491
489
1. IDEs will need to provide dedicated handling for this new `<script setup>` model in order to provide template expression type checking / props validation, etc.
492
490
493
-
As of now, [Volar](https://github.com/johnsoncodehk/volar) already provides full support for this RFC in VSCode, including all TypeScript related features. Its internals are also implemented as a landuage server that can theoretically be used in other IDEs.
491
+
As of now, [Volar](https://github.com/johnsoncodehk/volar) already provides full support for this RFC in VSCode, including all TypeScript related features. Its internals are also implemented as a landuage server that can theoretically be used in other IDEs.
494
492
495
493
2. ESLint rules like `no-unused-vars`. We will need a replacement rule in `eslint-plugin-vue` that takes both the `<script setup>` and `<template>` expressions into account.
496
494
@@ -563,11 +561,11 @@ The `SFCScriptBlock` returned by `compiledScript` also exposes a `bindings` obje
563
561
564
562
```html
565
563
<scriptsetup="props">
566
-
exportconstfoo=1
564
+
exportconstfoo=1
567
565
568
-
exportdefault {
569
-
props: ['bar'],
570
-
}
566
+
exportdefault {
567
+
props: ['bar']
568
+
}
571
569
</script>
572
570
```
573
571
@@ -586,7 +584,7 @@ This object can then be passed to the template compiler:
0 commit comments