vue3引入.vue文件以及.ts文件时提示找不到模块

1.找不到vue文件的,是因为ts无法解析我们的vue结尾的文件,所以需要在src目录下,

新建一个d.ts结尾的文件(可以叫env.d.ts)

然后里面这样写就可以

/// <reference types="vite/client" />

declare module '*.vue' {
	import { DefineComponent } from 'vue';
	// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
	const component: DefineComponent<{}, {}, any>;
	export default component;
}

// 环境变量 TypeScript的智能提示
interface ImportMetaEnv {
	VITE_APP_TITLE: string;
	VITE_APP_PORT: string;
	VITE_APP_BASE_API: string;
}

interface ImportMeta {
	readonly env: ImportMetaEnv;
}

2,找不到ts文件就更正常了,我们需要在tsconfig.json里面进行配置(没有的话就新建一个,在根src同级的目录下面)。就直接复制就完事了,

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "useDefineForClassFields": true,
    "moduleResolution": "node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "lib": ["esnext", "dom"],
    "baseUrl": "./",
    "paths": {
      "@": ["src"],
      "@/*": ["src/*"]
    }
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.d.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "*.ts"
  ],
  "references": [{ "path": "./tsconfig.node.json" }]
}

有的博主可能直接是在include里面加

"src/**/*.ts",

 这样是一种情况,这样让我们多级选目录时不报错,

但是还是会有'.xxx.ts'这样简单的路径导入,所以我们就必须加

  "*.ts"

猜你喜欢

转载自blog.csdn.net/qq_40606563/article/details/126068678