简体   繁体   English

Vue组件导入接口时如何避免TS2614错误

[英]How to avoid TS2614 error when importing interfaces from Vue components

I have defined an interface in my Vue component script section and when I tried to import it in another typescript file this error appear:我在我的 Vue 组件脚本部分定义了一个接口,当我尝试将它导入另一个 typescript 文件时,出现此错误:

TS2614: Module '"../pages/categories/alimentation/index.vue"' has no exported member 'BenefitDetails'. Did you mean to use 'import BenefitDetails from "../pages/categories/alimentation/index.vue"' instead

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'

export interface BenefitDetails {
  name: string
  frequency: string
  cost: number
  icon: string
  description: string
}

@Component
export default class MyComponent extends Vue {
  // my props ...

  mounted() {}
}
</script>

// Error in import below
import { BenefitDetails } from '~/pages/categories/alimentation/index.vue'

export function getBenefitFrequencyColor({ frequency }: BenefitDetails) {
  switch (frequency) {
    case 'Mensal':
      return 'blue'
    default:
      return 'yellow'
  }
}

I found a solution to VSCode in this question but I'am using webstorm我在这个问题中找到了 VSCode 的解决方案,但我正在使用 webstorm

UPDATE: I do not want to move the interface declaration to another file更新:我不想将接口声明移动到另一个文件

What IDE version do you use?你用的是什么IDE版本? Can't see any errors when using your code in 2020.3.1 (with the language service enabled in Settings | Languages & Frameworks | TypeScript ):在 2020.3.1 中使用您的代码时看不到任何错误(在Settings | Languages & Frameworks | TypeScript中启用了语言服务):

在此处输入图像描述

Try declaring BenefitDetails interface in de dedicated ts file (ie: benefitDetails.model.ts)尝试在专用 ts 文件中声明 BenefitDetails 接口(即:benefitDetails.model.ts)

Then import the BenefitDetails where needed然后在需要的地方导入 BenefitDetails

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vue 3 + TypeScript TS2614:模块 &#39;&quot;*.vue&quot;&#39; 没有导出成员 - Vue 3 + TypeScript TS2614: Module '"*.vue"' has no exported member TS2614:模块没有导出成员“NextHandleFunction” - TS2614: Module has no exported member 'NextHandleFunction' 为什么在使用 ES 模块语法导入解构 flatten-js 库的默认导出时得到 TS2614 而在 const-destructure 时却没有? - Why do I get a TS2614 when import-destructure the default export of flatten-js library using ES module syntax but not when const-destructure it? 模块 '"*.vue"' 没有导出的成员 'Hoge'。 您的意思是改用“从“*.vue”导入会议吗?ts(2614) - Module '"*.vue"' has no exported member 'Hoge'. Did you mean to use 'import Meeting from "*.vue"' instead?ts(2614) 如何在 Vue 中创建 .ts 组件? - How to create .ts components in Vue? Vue &amp; Typescript:访问子组件方法时如何避免 TS 错误 - Vue & Typescript: How to avoid TS error when accessing child component's methods Vue&TypeScript:在项目目录之外的TypeScript组件中实施导入时,如何避免错误TS2345? - Vue&TypeScript: how to avoid error TS2345 when import implemented in TypeScript component outside of project directory? Angular ts-从ngFor导入具有本地数据的组件 - Angular ts - importing components with local data from ngFor 将js文件导入ts时出现Typescript错误TS5055 - Typescript error TS5055 when importing js file into ts 在typescript中导入接口时出错 - Error with importing interfaces in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM