【问题标题】:Nuxt TypeScript error: nuxt:typescript Cannot find module '@/my-module'Nuxt TypeScript 错误:nuxt:typescript 找不到模块'@/my-module'
【发布时间】:2020-02-16 20:41:39
【问题描述】:

我已经使用来自https://typescript.nuxt.org 的说明使用 TypeScript 设置 Nuxt。过渡非常无缝且没问题,除了我无法摆脱nuxt:typescript“找不到模块”的错误,这显然是误报。

我的导入如下所示:

import InputField from '@/components/InputField.vue'

我尝试过使用~/ 并且不使用.vue 扩展名。没有任何效果。

我的tsconfig.json 看起来像这样:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "lib": ["esnext", "esnext.asynciterable", "dom"],
    "esModuleInterop": true,
    "allowJs": true,
    "sourceMap": true,
    "strict": true,
    "noEmit": true,
    "baseUrl": ".",
    "paths": {
      "~/*": ["src/*"],
      "~*": ["src/*"],
      "@/*": ["src/*"]
    },
    "types": ["@types/node", "@nuxt/types"]
  },
  "exclude": ["node_modules"]
}

我的nuxt.config.js 中有这个对象:

typescript: {
  typeCheck: {
    eslint: true,
    vue: true
  }
}

【问题讨论】:

  • 尝试改成"@/*": ["./src/*"]
  • @Aldarund 很遗憾,这并没有帮助。
  • 请在github上创建一个最小的复制品
  • 这里有一个修复,但它只涵盖.vue 文件,而不是.ts 文件导入:github.com/nuxt/typescript/issues/153
  • 是的,没错,不知道你没有 :)

标签: typescript vue.js nuxt.js tsconfig


【解决方案1】:

我在这里找到了解决方案:https://github.com/nuxt/typescript/issues/153#issuecomment-543010838

基本上,在您的根目录中创建一个ts-shim.d.ts,并包含以下内容:

declare module '*.vue' {
  import Vue from 'vue';
  export default Vue
}

至于您的tsconfig.json,请确保您有以下条目:

{
  "compilerOptions": {
    ...
    "baseUrl": ".",
    "paths": {
      "~/*": ["./*"],
      "@/*": ["./*"]
    }
  }
}

这解决了.vue 导入和.ts 导入的问题。

注意!现在绝对必须在 Vue 导入的末尾包含 .vue,否则导入将失败:

import SomeComponent from '~/components/SomeComponent.vue'

【讨论】:

  • "现在绝对必须包含 .vue" 帮助了我,谢谢
猜你喜欢
  • 2016-09-29
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 2020-08-15
  • 2021-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多