Nic*_*ick 6 typescript vuejs3 vite
~/utils/assets我在尝试在 Vue 3 SFC(单文件组件)文件中使用路径别名 ( ) 时遇到一个奇怪的问题。我正在使用Volar VS Code 插件,即使在使用Volar 插件启用“接管模式”后也会出现此问题。
Home.vue文件可以import { zipFromImages } from "~/utils/assets";毫无问题地引用。然而,Preview.vue与 \xe2\x80\x94 位于同一目录中的文件Home.vue\xe2\x80\x94 不是,并抛出 ts(2307) 错误:Cannot find module \'~/utils/assets\' or its corresponding type declarations.ts(2307)。Preview.vue文件(Vue SFC.vue文件)时,VS Code 底部显示“No tsconfig”。当我选择Home.vue文件时,它会正确显示“tsconfig.json”并按tsconfig.json预期链接到我的文件。还有其他人遇到过这些问题并找到解决方案吗?
\ntsconfig.json
{\n "compilerOptions": {\n "target": "es6",\n "lib": [\n "es6",\n "dom"\n ],\n "outDir": "./dist",\n "module": "esnext",\n "isolatedModules": true,\n "strict": true,\n "skipLibCheck": true,\n "esModuleInterop": true,\n "moduleResolution": "node",\n "noUnusedLocals": true,\n "noImplicitAny": true,\n "allowJs": true,\n "resolveJsonModule": true,\n "baseUrl": "./",\n "paths": {\n "@/*": [\n "./src/ui/*"\n ],\n "~/*": [\n "./src/ui/*"\n ]\n },\n "typeRoots": [\n "./node_modules/@types",\n "./node_modules/@figma"\n ]\n },\n "include": [\n "src/**/*.ts",\n "src/**/*.d.ts",\n "src/**/*.tsx",\n "src/**/*.vue"\n ],\n "exclude": [\n "node_modules",\n "dist"\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\nvite.config.ts
import { defineConfig } from "vite";\nimport vue from "@vitejs/plugin-vue";\nimport { viteSingleFile } from "vite-plugin-singlefile";\nimport path from "path";\n\nexport default defineConfig(({ mode }) => {\n const isDev = mode === "development";\n return {\n plugins: [vue(), viteSingleFile()],\n resolve: {\n alias: {\n "@": path.resolve(__dirname, "src/ui"),\n "~": path.resolve(__dirname, "src/ui"),\n },\n },\n build: {\n outDir: path.resolve(__dirname, "dist/ui"), // The output directory\n minify: !isDev,\n sourcemap: true,\n watch: isDev\n ? {\n exclude: ["dist", "node_modules"],\n }\n : null,\n target: "esnext",\n assetsInlineLimit: 100000000,\n chunkSizeWarningLimit: 100000000,\n cssCodeSplit: false,\n brotliSize: false,\n rollupOptions: {\n output: {\n manualChunks: () => "everything.js",\n },\n inlineDynamicImports: true,\n },\n },\n };\n});\nRun Code Online (Sandbox Code Playgroud)\nVS Code 和 VS Code 插件
\nNPM 包
\n