【问题标题】:How to avoid TS2614 error when importing interfaces from Vue components从Vue组件导入接口时如何避免TS2614错误
【发布时间】:2021-04-20 14:31:45
【问题描述】:

我在我的 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'
  }
}

我在this 问题中找到了 VSCode 的解决方案,但我正在使用 webstorm

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

【问题讨论】:

  • 请发布您的代码文本而不是屏幕截图
  • tsconfig.json 中,compilerOptions.allowSyntheticDefaultImports 是否设置为true?如果没有,请添加它,这应该可以解决问题。
  • 仅将此设置添加到 tsconfig.json 不起作用。使用此设置重新启动 IDE 是可行的,但我也注意到,无论对 tsconfig.json 进行任何更改,只要重新启动 IDE 问题就消失了
  • 重启 webstorm 为我解决了这个问题。

标签: typescript vue.js webstorm


【解决方案1】:

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

然后在需要的地方导入 BenefitDetails

【讨论】:

  • 这是一个有效的解决方案。但不是我想要的。我已使用此信息更新了问题
【解决方案2】:

您使用什么 IDE 版本?在 2020.3.1 中使用您的代码时看不到任何错误(在 Settings | Languages & Frameworks | TypeScript 中启用了语言服务):

【讨论】:

  • 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
  • 我不知道这是否相关,但我正在使用 Nuxt.js
  • 我刚刚重启了 Webstorm,一切都恢复正常了
猜你喜欢
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
相关资源
最近更新 更多