TypeScrip在vue中的使用----defineEmits

向父元素发送消息

之前的语法:

 在TS语法中,我们既要对defineEmits做类型约束,又要对emits做类型约束。

最主要是对defineEmits做一个泛型的约束。

//在泛型对象中,有几个事件就写几个约束
type emitsType = {
    //()中有n个参数,第一个固定的是e,其他有具体参数决定。具体的写法为(e:事件名,参数1:类型1,参数2:类型2):void
    (e: "getGift", getGift: string): void;
};

将上述定义好的泛型约束放到defineEmits中

type emitsType = {
    //()中有n个参数,第一个固定的是e,其他有具体参数决定。具体的写法为(e:事件名,参数1:类型1,参数2:类型2):void
    (e: "getGift", getGift: string): void;
};

const emit = defineEmits<emitsType>();
const hgift = ()=>{
    emit('getGift','柚柚')
}

emit中发送getGift的参数就必须是一个字符串了

posted @ 2024-10-01 14:25  洛飞  阅读(310)  评论(0)    收藏  举报