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>
相关推荐
去伪存真3 小时前
看我如何破解api接口文档定义空白, 还不想手动写接口TS类型定义的困局
前端·typescript
BillKu8 小时前
Vue3 + TypeScript 的 Hooks 实用示例
前端·vue.js·typescript
Joet11 小时前
elementPlus_upload组件二次封装,cos上传
vue.js·typescript
菜鸟una11 小时前
【Taro3.x + Vue3】搭建微信小程序
前端·vue.js·typescript·taro
kurcp14 小时前
vue3 antdesign上传解析excel
前端·typescript·excel
DCTANT1 天前
【原创】vue-element-admin-plus完成确认密码功能,并实时获取Form中表单字段中的值
前端·javascript·vue.js·elementui·typescript
黑土豆1 天前
TypeScript技术系列13:深入理解配置文件tsconfig.json
前端·javascript·typescript
羊聪1 天前
SSE数据流的基本实现与处理:示例与解析
前端·typescript
@PHARAOH1 天前
WHAT - React 两个重要的 Typescript 类型:ReactNode vs JSX.Element
javascript·react.js·typescript
我自纵横20231 天前
Vue 3 中 ref 与 reactive 的对比
前端·javascript·vue.js·typescript·前端框架·html5