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

How to avoid TS2614 error when importing interfaces from Vue components

提问人:Michael Pacheco 提问时间:1/15/2021 最后编辑:Dave MackeyMichael Pacheco 更新时间:3/30/2023 访问量:3481

问:

我已经在我的 Vue 组件脚本部分定义了一个接口,当我尝试将其导入另一个打字稿文件时,出现了此错误:

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'
  }
}

我在这个问题中找到了 VSCode 的解决方案,但我使用的是 WebStorm。

更新:我不想将接口声明移动到另一个文件

Typescript vue.js webstorm

评论

1赞 mousetail 1/15/2021
请发布您的代码文本,而不是屏幕截图
0赞 tao 1/15/2021
在 中,设置为 ?如果没有,请添加它,这应该可以解决问题。tsconfig.jsoncompilerOptions.allowSyntheticDefaultImportstrue
0赞 Michael Pacheco 1/16/2021
仅将此设置添加到不起作用。使用此设置重新启动 IDE 是有效的,但我也注意到,无论对tsconfig.jsontsconfig.json
0赞 Boris K 10/25/2021
重新启动 webstorm 为我解决了这个问题。
0赞 RoduanKD 4/26/2023
我在 WebStorm 2023 上,问题再次出现,尽管 WebStorm 2021 中不存在

答:

0赞 David ROSEY 1/15/2021 #1

尝试在专用的 ts 文件中声明 BenefitDetails 接口(即:benefitDetails.model.ts)

然后在需要时导入 BenefitDetails

评论

0赞 Michael Pacheco 1/15/2021
这是一个有效的解决方案。但不是我想要的。我已经用这个信息更新了问题
1赞 lena 1/16/2021 #2

您使用的是哪个 IDE 版本?在 2020.3.1 中使用代码时看不到任何错误(在“设置”|”语言和框架 |打字稿):

enter image description here

评论

0赞 Michael Pacheco 1/16/2021
WebStorm 2020.3.1 Build #WS-203.6682.155, built on December 28, 2020 Licensed to Michael Douglas Pacheco Gonçalves Dias Subscription is active until February 5, 2021. For educational use only. Runtime version: 11.0.9.1+11-b1145.63 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Linux 5.8.0-36-generic GC: ParNew, ConcurrentMarkSweep Memory: 1981M Cores: 8 Registry: ide.balloon.shadow.size=0 Non-Bundled Plugins: com.chrisrm.idea.MaterialThemeUI, com.mallowigi, com.markskelton.one-dark-theme, ru.adelf.idea.dotenv, com.vermouthx.idea Current Desktop: ubuntu:GNOME
0赞 Michael Pacheco 1/16/2021
我不知道这是否相关,但我正在使用 Nuxt.js
2赞 Michael Pacheco 1/16/2021
我刚刚重新启动了 Webstorm,everthing 又开始工作了