vue3中使用typescript小结

ref

typescript 复制代码
const msg = ref<string>('')
const nme = ref<string | null>(null)
const isDone = ref<boolean>(false)
// 普通的button
const button = ref<HTMLButtonElement | null>(document.querySelector('button'))
// const button: HTMLButtonElement | null = document.querySelector('button')
// tableBody
const tableBody: HTMLTableElement | null = document.querySelector('#table-body')

const list = ref<Array<string>>(['apple', 'pear'])
const statusOptions = ref<Array<Object>>([
  {
    label: '启用',
    value: '0'
  },
  {
    label: '停用',
    value: '1'
  }
])

接口interface

typescript 复制代码
interface DogType {
    id: string
    url: string
    height: number
    width: number
    test?: boolean
}

class Dog implements DogType {
    id: string
    url: string
    height: number
    width: number

    constructor(id: string, url: string, height: number, width: number) {
        this.id = id
        this.url = url
        this.height = height
        this.width = width
    }
}

类 class

typescript 复制代码
class ShowImage {
    public static addData(data: DogType): void {
        const dog: Dog = new Dog(data.id, data.url, data.height, data.width)
        const tableRow: HTMLTableRowElement = document.createElement('tr')
        tableRow.innerHTML = `
            <td>${cat.id}</td>
            <td><img src="${cat.url}" /></td>
            <td>${cat.height.toString()}</td>
            <td>${cat.width.toString()}</td>
            <td>${cat.url}</td>
            <td><a href="#">X</a></td>
        `
        tableBody?.appendChild(tableRow)
    }
    public static deleteData(deleteButton: HTMLAnchorElement): void {
        const td = deleteButton.parentElement as HTMLTableCellElement
        const tr = td.parentElement as HTMLTableRowElement
        tr.remove();
    }
}

promise

typescript 复制代码
async function getJSON<T>(url: string): Promise<T> {
    const response: Response = await fetch(url)
    const data: Promise<T> = await response.json()
    return data
}

async function getData(): Promise<void> {
    try {
        const dataJson: CatType[] = await getJSON<CatType[]>(url)
        const dataJson: CatType = json[0]
        ShowImage.addData(data)
    }
    catch (error: Error | unknown) {
        let message = ref<string>()
        if (error instanceof Error) {
            message = error.message
        } else {
            message = String(error)
        }
        console.log(error)
    }
}

button?.addEventListener<'click'>('click', getData);

tableBody?.addEventListener<'click'>('click', (e: MouseEvent) => {
    ShowImage.deleteData(<HTMLAnchorElement>e.target);
});

reactive

typescript 复制代码
interface Info {
	name: string
	age: number
	address: string
	sex: string
	phone?: string
}

const infoRef = reactive<Info>({
	name: 'hello',
	age: 20,
	address: 'Beijing',
	sex: '男',
	phone: '123'
})

组件实例

typescript 复制代码
import Dialog from '@/components/Dialog/index.vue'
// typescript内置的 InstanceType + typeof
// 定义组件的实例
const dialogRef = ref<InstanceType<typeof Dialog | null>>(null)

defineProps

typescript 复制代码
interface Props {
	name: string
	age: number
	phone?: string
}

defineProps<Props>()
typescript 复制代码
interface Emits {
  handleValChange: (val: string) => ()
  handleValUpdate: (val: string) => ()
}
const emits = defineEmits<Emits>()

ref 获取dom节点

typescript 复制代码
// 初始节点可能是 null,所以要补全 null 类型
const ele  = ref<HTMLDivElement | null>(null)
<div ref="ele"></div>
相关推荐
今天头发还在吗8 小时前
【React】动态SVG连接线实现:图片与按钮的可视化映射
前端·javascript·react.js·typescript·前端框架
冷冷的菜哥9 小时前
react多文件分片上传——支持拖拽与进度展示
前端·react.js·typescript·多文件上传·分片上传
Kisang.15 小时前
【HarmonyOS】窗口管理实战指南
前端·华为·typescript·harmonyos·鸿蒙
Dajiaonew1 天前
Vue3 + TypeScript 一篇文章 后端变全栈
前端·javascript·typescript
敲敲敲敲暴你脑袋2 天前
用3Dmol.js展示3D分子结构
typescript·webgl·数据可视化
还是大剑师兰特2 天前
TypeScript 面试题及详细答案 100题 (11-20)-- 基础类型与类型操作
typescript·大剑师·typescript教程·typescript面试题
用户47949283569152 天前
TypeScript 和 JavaScript 的 'use strict' 有啥不同
前端·javascript·typescript
用户47949283569152 天前
还不知道'use strict'的作用?这篇文章给你讲清楚
前端·javascript·typescript
乐影2 天前
TS 模板字符串类型:从基础到进阶的类型编程魔法
前端·typescript
柯南二号3 天前
【大前端】 TypeScript vs JavaScript:全面对比与实践指南
前端·javascript·typescript