vue3与ts的组合式API

vue3与ts的组合式API

props

1.基本props
vue 复制代码
<script setup lang="ts">
const props = defineProps<{
  foo: string
  bar?: number
}>()
</script>
2.抽离props
vue 复制代码
<script setup lang="ts">
interface Props {
  foo: string
  bar?: number
}

const props = defineProps<Props>()
</script>
3.抽离和导入type
vue 复制代码
<script setup lang="ts">
import type { Props } from './foo'

const props = defineProps<Props>()
</script>
4.默认值props
tsx 复制代码
export interface Props {
  msg?: string
  labels?: string[]
}

const props = withDefaults(defineProps<Props>(), {
  msg: 'hello',
  labels: () => ['one', 'two']
})
5.复杂props
vue 复制代码
<script setup lang="ts">
interface Book {
  title: string
  author: string
  year: number
}

const props = defineProps<{
  book: Book
}>()
</script>

emits

一个类型字面量,其中键是事件名称,值是数组或元组类型,表示事件的附加接受参数。示例使用了具名元组,因此每个参数都可以有一个显式的名称。

vue 复制代码
<script setup lang="ts">
// 基于类型
const emit = defineEmits<{
  (e: 'change', id: number): void
  (e: 'update', value: string): void
}>()

// 3.3+: 可选的、更简洁的语法
const emit = defineEmits<{
  change: [id: number]
  update: [value: string]
}>()
</script>

ref()

1.自动推导
typescript 复制代码
import { ref } from 'vue'

// 推导出的类型:Ref<number>
const year = ref(2020)

// => TS Error: Type 'string' is not assignable to type 'number'.
year.value = '2020'
2.复杂ref
typescript 复制代码
import { ref } from 'vue'
import type { Ref } from 'vue'

const year: Ref<string | number> = ref('2020')

year.value = 2020 // 成功!

个人建议使用第二种,字少了,且无需导入import type { Ref } from 'vue'

typescript 复制代码
// 得到的类型:Ref<string | number>
const year = ref<string | number>('2020')

year.value = 2020 // 成功!

注意:如果你指定了一个泛型参数但没有给出初始值 ,那么最后得到的就将是一个包含 undefined 的联合类型:

typescript 复制代码
// 推导得到的类型:Ref<number | undefined>
const n = ref<number>()

recative()

1.自动推导
typescript 复制代码
import { reactive } from 'vue'

// 推导得到的类型:{ title: string }
const book = reactive({ title: 'Vue 3 指引' })
2.复杂reactive
typescript 复制代码
import { reactive } from 'vue'

interface Book {
  title: string
  year?: number
}

const book: Book = reactive({ title: 'Vue 3 指引' })

注意: 不推荐使用 reactive()泛型参数,因为处理了深层次 ref 解包的返回值与泛型参数的类型不同。

错误示例:

typescript 复制代码
import { reactive } from 'vue'

interface Book {
  title: string;
  year?: number;
}

// 不推荐:使用了泛型参数,但返回的类型与Book不同
const book: Book = reactive<Book>({ title: 'Vue 3 指引' });

正确示例,使用toRefs()

typescript 复制代码
import { reactive, toRefs } from 'vue'

interface Book {
  title: string;
  year?: number;
}

// 创建一个响应式的Book对象
const bookReactive = reactive({ title: 'Vue 3 指引' });

// 使用toRefs()将每个属性转换为Ref,确保类型安全
const bookRefs = toRefs(bookReactive);

// 现在bookRefs是一个Record类型,其中的属性具有与Book相同的类型
const book: { title: Ref<string>; year?: Ref<number | undefined> } = bookRefs;

// 或者,如果你知道你只会在模板中使用,可以使用类型断言
const bookWithAssertion = {
  title: bookRefs.title as Ref<string>,
  year: bookRefs.year as Ref<number | undefined>,
} as const;

computed()

1.自动推导
typescript 复制代码
import { ref, computed } from 'vue'

const count = ref(0)

// 推导得到的类型:ComputedRef<number>
const double = computed(() => count.value * 2)

// => TS Error: Property 'split' does not exist on type 'number'
const result = double.value.split('')
2.显式指定
typescript 复制代码
const double = computed<number>(() => {
  // 若返回值不是 number 类型则会报错
})

为事件处理函数标注类型

**在处理原生 DOM 事件时,应该为我们传递给事件处理函数的参数正确地标注类型。**让我们看一下这个例子:

typescript 复制代码
<script setup lang="ts">
function handleChange(event) {
  // `event` 隐式地标注为 `any` 类型
  console.log(event.target.value)
}
</script>

<template>
  <input type="text" @change="handleChange" />
</template>

没有类型标注时,这个 event 参数会隐式地标注为 any 类型。这也会在 tsconfig.json 中配置了 "strict": true"noImplicitAny": true 时报出一个 TS 错误。因此,建议显式地为事件处理函数的参数标注类型。此外,你在访问 event 上的属性时可能需要使用类型断言:

typescript 复制代码
function handleChange(event: Event) {
  console.log((event.target as HTMLInputElement).value)
}

为 provide / inject 标注类型

typescript 复制代码
const foo = inject<string>('foo') // 类型:string | undefined ???

为模板引用标注类型

模板引用需要通过一个显式指定的泛型参数和一个初始值 null 来创建:

typescript 复制代码
<script setup lang="ts">
import { ref, onMounted } from 'vue'

const el = ref<HTMLInputElement | null>(null)

onMounted(() => {
  el.value?.focus()
})
</script>

<template>
  <input ref="el" />
</template>

可以通过类似于 MDN 的页面来获取正确的 DOM 接口。https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLInputElement

注意为了严格的类型安全,有必要在访问 el.value 时使用可选链或类型守卫。这是因为直到组件被挂载前,这个 ref 的值都是初始的 null,并且在由于 v-if 的行为将引用的元素卸载时也可以被设置为 null

为组件模板引用标注类型

有时,你可能需要为一个子组件添加一个模板引用,以便调用它公开的方法。举例来说,我们有一个 MyModal 子组件,它有一个打开模态框的方法:

vue 复制代码
<!-- MyModal.vue -->
<script setup lang="ts">
import { ref } from 'vue'

const isContentShown = ref(false)
const open = () => (isContentShown.value = true)

defineExpose({
  open
})
</script>

为了获取 MyModal 的类型,我们首先需要通过 typeof 得到其类型,再使用 TypeScript 内置的 InstanceType 工具类型来获取其实例类型:

vue 复制代码
<!-- App.vue -->
<script setup lang="ts">
import MyModal from './MyModal.vue'

const modal = ref<InstanceType<typeof MyModal> | null>(null)

const openModal = () => {
  modal.value?.open()
}
</script>

如果组件的具体类型无法获得,或者你并不关心组件的具体类型,那么可以使用 ComponentPublicInstance。这只会包含所有组件都共享的属性,比如 $el

typescript 复制代码
import { ref } from 'vue'
import type { ComponentPublicInstance } from 'vue'

const child = ref<ComponentPublicInstance | null>(null)

官网链接

相关推荐
爱上妖精的尾巴2 小时前
8-5 WPS JS宏 match、search、replace、split支持正则表达式的字符串函数
开发语言·前端·javascript·wps·jsa
小温冲冲2 小时前
通俗且全面精讲单例设计模式
开发语言·javascript·设计模式
意法半导体STM323 小时前
【官方原创】FDCAN数据段波特率增加后发送失败的问题分析 LAT1617
javascript·网络·stm32·单片机·嵌入式硬件·安全
为什么不问问神奇的海螺呢丶3 小时前
n9e categraf redis监控配置
前端·redis·bootstrap
云飞云共享云桌面3 小时前
推荐一些适合10个SolidWorks设计共享算力的服务器硬件配置
运维·服务器·前端·数据库·人工智能
Liu.7743 小时前
vue开发h5项目
vue.js
咔咔一顿操作3 小时前
轻量无依赖!autoviwe 页面自适应组件实战:从安装到源码深度解析
javascript·arcgis·npm·css3·html5
刘联其4 小时前
.net也可以用Electron开发跨平台的桌面程序了
前端·javascript·electron
韩曙亮4 小时前
【jQuery】jQuery 选择器 ④ ( jQuery 筛选方法 | 方法分类场景 - 向下找后代、向上找祖先、同级找兄弟、范围限定查找 )
前端·javascript·jquery·jquery筛选方法
前端 贾公子4 小时前
Node.js 如何处理 ES6 模块
前端·node.js·es6