vue3.x+Ts组件封装

Posted 朱丽叶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue3.x+Ts组件封装相关的知识,希望对你有一定的参考价值。

// 1
    import { defineComponent, PropType } from "vue"
    import { IFromItem } from "./types"

    props: {
        fromItem: {
          // 给传递过来的数据进行类型限制  需要在vue中引入PropType 重新断言为自己设置的类型。
          type: Array as PropType<IFromItem[]>,
          // 在vue+ts中props给定默认值为数组或者对象时,使用箭头函数
          default: () => []
        },
        labelWidth: {
          type: String,
          default: "100px"
        },
      },

// 2. 定义数据类型
    export interface IFromItem {
      label: string
      type: "input" | "password" | "select" | "datepicker"
      placeholder?: string
      // 针对select
      options?: any[]
      // 针对特殊的属性
      otherOptions?: any
    }

以上是关于vue3.x+Ts组件封装的主要内容,如果未能解决你的问题,请参考以下文章