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
{{ message }}
This repository was archived by the owner on Aug 8, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: src/api/sfc-script-setup.md
+70-71Lines changed: 70 additions & 71 deletions
Original file line number
Diff line number
Diff line change
@@ -2,38 +2,37 @@
2
2
sidebarDepth: 1
3
3
---
4
4
5
-
<!-- TODO: translation -->
6
-
# SFC `<script setup>`
5
+
# 单文件组件 `<script setup>`
7
6
8
-
`<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:
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:
@@ -56,9 +55,9 @@ import { capitalize } from './helpers'
56
55
</template>
57
56
```
58
57
59
-
## Reactivity
58
+
## 响应式
60
59
61
-
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:
Values in the scope of `<script setup>`can also be used directly as custom component tag names:
76
+
`<script setup>`范围里的值也能被直接作为自定义组件的标签名称使用:
78
77
79
78
```vue
80
79
<script setup>
@@ -86,11 +85,11 @@ import MyComponent from './MyComponent.vue'
86
85
</template>
87
86
```
88
87
89
-
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, we 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:
@@ -132,9 +131,9 @@ import * as Form from './form-components'
132
131
</template>
133
132
```
134
133
135
-
## `defineProps`and`defineEmits`
134
+
## `defineProps`和`defineEmits`
136
135
137
-
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>`:
-`defineProps`and`defineEmits`are **compiler macros** only usable inside `<script setup>`. They do not need to be imported, and are compiled away when `<script setup>`is processed.
-`defineProps`and`defineEmits`provide proper type inference based on the options passed.
153
+
-`defineProps`和`defineEmits`在选项传入后,会提供恰当的类型推断。
155
154
156
-
-The options passed to `defineProps`and`defineEmits`will be hoisted out of setup into module scope. Therefore, the options cannot reference local variables declared in setup scope. Doing so will result in a compile error. However, it _can_ reference imported bindings since they are in the module scope as well.
Components using `<script setup>`are **closed by default** - i.e. the public instance of the component, which is retrieved via template refs or `$parent`chains, will **not** expose any of the bindings declared inside `<script setup>`.
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).
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:
`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.
194
+
`useSlots`和`useAttrs`是真实的运行时函数,它会返回与 `setupContext.slots`和`setupContext.attrs` 等价的值,同样也能在普通的组合式 API 中使用。
196
195
197
-
## Usage alongside normal `<script>`
196
+
## 与普通的 `<script>` 一起使用
198
197
199
-
`<script setup>`can be used alongside normal `<script>`. A normal `<script>`may be needed in cases where we need to:
const post = await fetch(`/api/post/1`).then(r => r.json())
229
228
</script>
230
229
```
231
230
232
-
In addition, the awaited expression will be automatically compiled in a format that preserves the current component instance context after the `await`.
231
+
另外,await 的表达式会自动编译成在 `await` 之后保留当前组件实例上下文的格式
233
232
234
-
:::warning Note
235
-
`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.
-When using type declaration, the equivalent runtime declaration is automatically generated from static analysis to remove the need for double declaration and still ensure correct runtime behavior.
-In dev mode, the compiler will try to infer corresponding runtime validation from the types. For example here `foo: String` is inferred from the `foo: string` type. If the type is a reference to an imported type, the inferred result will be `foo: null` (equal to `any`type) since the compiler does not have information of external files.
-The emitted code is still TypeScript with valid typing, which can be further processed by other tools.
263
+
-生成的代码仍然是有着类型的 Typescript 代码,它会在后续的流程中被其它工具处理。
265
264
266
-
-As of now, the type declaration argument must be one of the following to ensure correct static analysis:
265
+
-截至目前,类型声明参数必须是以下内容之一,以确保正确的静态分析:
267
266
268
-
-A type literal
269
-
-A reference to an interface or a type literal in the same file
267
+
-类型字面量
268
+
-在同一文件中的接口或类型字面量的引用
270
269
271
-
Currently complex types and type imports from other files are not supported. It is theoretically possible to support type imports in the future.
270
+
现在还不支持复杂的类型和从其它文件进行类型导入。理论上来说,将来是可能实现类型导入的。
272
271
273
-
### Default props values when using type declaration
272
+
### 使用类型声明时的默认 props 值
274
273
275
-
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.
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