这是因为在 Vue 3 中,默认的 TypeScript 配置不支持 'import.meta.url'。要解决这个问题,你需要手动调整 TypeScript 配置。

在项目根目录下创建一个名为 'tsconfig.json' 的文件,并添加以下内容:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "allowJs": true,
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "jsx": "preserve",
    "sourceMap": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    },
    "types": ["vite/client"]
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
  "exclude": ["node_modules"]
}

这将覆盖默认的 TypeScript 配置,并添加了 'types': ['vite/client'],以确保 Vite 的类型声明被正确加载。

然后,在你的 TypeScript 文件中,你就可以使用 'import.meta.url' 了。

Vue3 Vite 中使用 import.meta.url 的 TypeScript 配置

原文地址: https://www.cveoy.top/t/topic/pfLJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录