解决 Couldn't resolve 'types' can only be used in a .ts file. Vetur(8010)

5 min read

这个问题通常出现在使用 Vue.js 和 TypeScript 时。解决方法是:

  1. 确保在项目根目录下有一个 tsconfig.json 文件,如果没有可通过 npm install typescript -g 安装 TypeScript。

  2. 在 tsconfig.json 文件中添加以下设置:

    {
      "compilerOptions": {
        "target": "es5",
        "module": "esnext",
        "allowSyntheticDefaultImports": true,
        "strict": false,
        "esModuleInterop": true
      },
      "include": ["src/**/*"]
    }
    
  3. 在项目中选择文件的右下角,点击选择默认语言模式,将其设置为 TypeScript。

  4. 如果使用了 Vetur 插件,请确保更新至最新版本。如果更新后仍存在问题,可以尝试在项目根目录下创建一个 vueconfig.json 文件,将其内容设置为:

    {
      "compilerOptions": {
        "types": ["vuetify"]
      }
    }
    

以上方法应该可以解决这个问题。如果还有问题可以尝试重新安装相关插件或重新安装 TypeScript。