@@ -68,7 +68,7 @@ const props = defineProps<Props>()
6868
6969### Props 解构默认值 {#props-default-values}
7070
71- 当使用基于类型的声明时,我们失去了为 props 声明默认值的能力。可以通过使用[ 响应式 Props 解构] ( /guide/components/props#reactive-props-destructure ) 解决这个问题。 <sup class =" vt-badge " data-text =" 3.5+ " />:
71+ 当使用基于类型的声明时,我们失去了为 props 声明默认值的能力。可以通过使用[ 响应式 Props 解构] ( /guide/components/props#reactive-props-destructure ) 解决这个问题。 <sup class =" vt-badge " data-text =" 3.5+ " />:
7272
7373``` ts
7474interface Props {
@@ -96,7 +96,7 @@ const props = withDefaults(defineProps<Props>(), {
9696这将被编译为等效的运行时 props ` default ` 选项。此外,` withDefaults ` 帮助程序为默认值提供类型检查,并确保返回的 props 类型删除了已声明默认值的属性的可选标志。
9797
9898::: info
99- 请注意,在使用 ` withDefaults ` 时,默认值的可变引用类型( 如数组或对象) 应该在函数中进行包装,以避免意外修改和外部副作用。这样可以确保每个组件实例都会获得自己默认值的副本。当使用解构时,这** 不** 是必要的。
99+ 请注意,在使用 ` withDefaults ` 时,默认值的可变引用类型 ( 如数组或对象) 应该在函数中进行包装,以避免意外修改和外部副作用。这样可以确保每个组件实例都会获得自己默认值的副本。当使用解构时,这** 不** 是必要的。
100100:::
101101
102102### 非 ` <script setup> ` 场景下 {#without-script-setup}
@@ -371,16 +371,16 @@ const foo = inject('foo') as string
371371
372372## 为模板引用标注类型 {#typing-template-refs}
373373
374- 使用 Vue 3.5 和 ` @vue/language-tools ` 2.1( 为 IDE 语言服务和 ` vue-tsc ` 提供支持),在单文件组件中, ` useTemplateRef() ` 创建的 ref 类型可以根据匹配的 ` ref ` 属性使用的元素 ** 自动推断** 为静态 ref。
374+ 在 Vue 3.5 和 @vue/language-tools 2.1 ( 为 IDE 语言服务和 vue-tsc 提供支持) 中,在 SFC 中由 ` useTemplateRef() ` 创建的 ref 类型可以基于匹配的 ref attribute 所在的元素 ** 自动推断** 为静态类型。
375375
376- 在无法进行自动推断的情况下,仍然可以通过通用参数将模板 ref 转换为显式类型。
376+ 在无法自动推断的情况下,仍然可以通过泛型参数将模板 ref 转换为显式类型。
377377
378378``` ts
379379const el = useTemplateRef <HTMLInputElement >(null )
380380```
381381
382382<details >
383- <summary >在 3.5 之前的版本使用 </summary >
383+ <summary >3.5 前的用法 </summary >
384384
385385模板引用需要通过一个显式指定的泛型参数和一个初始值 ` null ` 来创建:
386386
@@ -408,11 +408,11 @@ onMounted(() => {
408408
409409## 为组件模板引用标注类型 {#typing-component-template-refs}
410410
411- 通过 Vue 3.5 和 ` @vue/language-tools ` 2.1(用于 IDE 语言服务和 ` vue-tsc ` )自动生成推断的 refs 类型,这将应用在基于 ` ref ` 属性使用的元素或组件的静态 refs 上,故在 SFC 中使用 ` useTemplateRef() ` 会 ** 自动推断** ref 类型 。
411+ 在 Vue 3.5 和 @vue/language-tools 2.1 (为 IDE 语言服务和 vue-tsc 提供支持) 中,在 SFC 中由 ` useTemplateRef() ` 创建的 ref 类型可以基于匹配的 ref attribute 所在的元素 ** 自动推断** 为静态类型 。
412412
413- 在无法自动推断的情况下( 如非 SFC 使用或动态组件),您可以通过泛型参数将模板 ref 强制转换为显式类型。
413+ 在无法自动推断的情况下 ( 如非 SFC 使用或动态组件),仍然可以通过泛型参数将模板 ref 强制转换为显式类型。
414414
415- 为了获取导入组件的实例类型,我们需要先通过 ` typeof ` 获取其类型,然后使用 TypeScript 的内置 ` InstanceType ` 实用程序提取其实例类型 :
415+ 为了获取导入组件的实例类型,我们需要先通过 ` typeof ` 获取其类型,然后使用 TypeScript 的内置 ` InstanceType ` 工具提取其实例类型 :
416416
417417``` vue{5}
418418<!-- App.vue -->
@@ -475,4 +475,4 @@ const openModal = () => {
475475</script>
476476```
477477
478- Note that with ` @vue/language-tools ` 2.1+, static template refs' types can be automatically inferred and the above is only needed in edge cases.
478+ 请注意在 ` @vue/language-tools ` 2.1 以上版本中,静态模板 ref 的类型可以被自动推导,上述这些仅在极端情况下需要。
0 commit comments