首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue 3- Composition - Typescript发射的->类型

Vue 3- Composition - Typescript发射的->类型
EN

Stack Overflow用户
提问于 2021-03-01 23:17:54
回答 3查看 2.6K关注 0票数 2

我第一次使用Typescript开始使用VUE 3和组合api。我有如下的设置方法:setup(props: { widgetType: string; displayType: string; trigger: number }, { emit })现在,当我构建这个文件时,我得到错误“绑定元素'emit‘隐式具有'any’类型。”我不知道如何解决这个问题。我尝试了网络上的不同解决方案,但都不起作用。有人能帮我吗?致敬Chris

EN

回答 3

Stack Overflow用户

发布于 2021-04-22 06:39:51

您需要使用defineComponent方法定义组件,并将正确的类型应用于props。如果您使用的是单个文件组件,则如下所示

代码语言:javascript
复制
<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>
票数 2
EN

Stack Overflow用户

发布于 2021-07-23 20:34:26

例如,使用组件"App“和可组合的"useModal”正确键入

应用程序:

代码语言:javascript
复制
<script lang="ts"> 
 import {defineComponent, SetupContext, ComponentPropsOptions} from 'vue';
 import {useModal} from '@/composable/useModal';
                    
 export default defineComponent({
   name: 'App',
   setup(props: ComponentPropsOptions, {emit}: SetupContext) {
     const openModal = useModal({emit});
                    
     return {openModal};
   }
});
</script>

useModal:

代码语言:javascript
复制
import {SetupContext} from 'vue';

interface UseModalProps {
  emit: SetupContext['emit'];
}

export function useModal({emit}: UseModalProps) {
  function openModal({title, type}: {title: string; type: string}): void {
    emit('openModal', {title, type});
  }

  return {openModal};
}
票数 0
EN

Stack Overflow用户

发布于 2021-03-01 23:43:23

要进行快速修复,您可以执行以下操作:

代码语言:javascript
复制
setup(props: { widgetType: string; displayType: string; trigger: number }, { emit }: { emit: Function | (eventName: string, payload: any) => void })

但我建议您使用整个组件更新您的问题,以便我们能够了解问题的根本原因

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66424746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档