首页 > 解决方案 > Vue 3 - Composition Api - Typescript -> 发射类型

问题描述

我从 VUE 3 和第一次使用 Typescript 的组合 api 开始。我有如下设置方法: setup(props: { widgetType: string; displayType: string; trigger: number }, { emit }) 现在,当我构建此文件时,我收到错误“绑定元素'emit'隐含地具有'any'类型。”。我不知道如何解决这个问题。我尝试了来自网络的不同解决方案,但没有任何效果。有谁能够帮我?问候克里斯

标签: typescriptvue.jscomposition

解决方案


您需要使用该defineComponent方法定义您的组件并将正确的类型应用于道具。如果您使用的是单个文件组件,它看起来像这样

<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
    props: {
        widgetType: {
            type: String,
            required: true
        },
        displayType: {
            type: String,
            required: true
        },
        trigger: {
            type: number,
            required: true
        }
    },

    setup(props, { emit }) {

        // Rest of your setup code here
    }
});
</script>

推荐阅读