【问题标题】:Module has no exported Member Vue3模块没有导出的成员 Vue3
【发布时间】:2021-07-08 16:32:48
【问题描述】:

我需要一些关于 Vue 组件的帮助。

我收到此错误:

Failed to compile.

src/components/Btn/Btn.vue:11:14
TS2305: Module '"../../typings/button-type"' has no exported member 'ButtonType'.
     9 |     import { defineComponent, computed } from '@vue/runtime-core';
  > 11 |     import { ButtonType } from '@/typings/button-type';
       |              ^^^^^^^^^^
    12 | // components
    13 |
    14 |     export default defineComponent({

我正在尝试在单独的文件中在 Ts 中创建自己的类型并将其导入到我的组件中。

类型:

export namespace ButtonType {

export type Button = {
size: Size;
type: Type;
 }
}

type Size = 'sm' | 'md' | 'lg';
type Type = 'primary' | 'accent' | 'subtle';

在我的组件中,我有以下内容:

   import { ButtonType } from '@/typings/button-type';
// components

export default defineComponent({
    name: 'Btn',
    components: {},
    props: {
        size: {
            default: 'md',
        } as ButtonType.Button.size,
        type: {
            default: 'primary',
        } as ButtonType.Button.type,
    }

我试过 ButtonType.Button['size'] 但也没有用。

我有一些计算数据等,但与案例无关。主要思想是创建一个类型,以便在为按钮组件设置错误的大小或类型时识别错误。

知道发生了什么吗?

【问题讨论】:

    标签: typescript vue.js vue-component


    【解决方案1】:

    Vue 使用 PropType 表示道具类型

    import { defineComponent, PropType } from 'vue'
    import { ButtonType } from '@/typings/button-type';
    
    export default defineComponent({
        props: {
            size: {
                default: "md",
                type: String as PropType<ButtonType['Button']['size']>
            },
            type: {
                default: "primary",
                type: String as PropType<ButtonType['Button']['type']>
            }
        }
      }
    })
    
    

    【讨论】:

    • 我已经尝试过,但出现以下错误:TS2352:将“string”类型转换为“PropType”类型可能是一个错误,因为两种类型都没有充分重叠。如果这是故意的,请先将表达式转换为“未知”。
    • @PaulB 对不起,我犯了一些错误,请尝试更新的答案。
    • 我不得不更改 ButtonType,因为它是一个命名空间,所以我不能将它与 PropType 一起使用。现在我只将 ButtonType 导出为一种类型,并像你一样引用它,但出现另一个错误 Module '"../../typings/button-type"' has no exported member 'Button'. 我不确定发生了什么
    • 我已经尝试了一些东西。显然,如果我只使用 Size 和 Type 作为类型,它可以工作export type Size = 'sm' | 'md' | 'lg' 但导出按钮它不会
    猜你喜欢
    • 1970-01-01
    • 2017-05-20
    • 2017-05-07
    • 2017-08-18
    • 1970-01-01
    • 2017-09-09
    • 2022-01-12
    • 2021-12-19
    相关资源
    最近更新 更多