TS变量定义总结

1、公共文件:scr/interface/common.ts

       定义一个公共的接口

  export interface option {
        value:any, //任意类型
        label:string,
        id:string | number  //数字或字符串类型
        name?:string        // ?-非必填
        [propname:string]:any   //key值为string类型的,任意类型

  }

.vue文件引入:import { option } ‘@/interface/common’

2、.vue文件单独定义

interface option {
        value:any, //任意类型
        label:string,
        id:string | number  //数字或字符串类型
        name?:string        // ?-非必填
        [propname:string]:any   //key值为string类型的,任意类型

  }

3、定义字段类型:

3.1、ref/reactive绑定数据定义

const  id=ref<string>('')

const  status=ref<string | number>('')

const obj=ref<option>({})

const arr=ref<option[]>([])

const state = reactive({
     list: [] as Array<number>   
})

3.2、for循环定义:

arr.forEach((item:option)=>{
    ...
})

3.3、let定义

let list:option[]=[]

3.4、函数传值定义

const fn=(e:arry<option>,id:string,obj:option)=>{
    ...
}

猜你喜欢

转载自blog.csdn.net/weixin_38961329/article/details/127572804