【vue3+ts】@设置别名

新建的vue3+ts项目,建路由的时候报错:

【vue3+ts】@设置别名_第1张图片
在vite.config.ts中新增如下代码:
【vue3+ts】@设置别名_第2张图片

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path"
// https://vitejs.dev/config/
export default defineConfig({
  resolve:{
    alias:{
      "@":path.resolve(__dirname,'src')
    }
  },
  plugins: [vue()],
})

在tsconfig.json中添加代码:
【vue3+ts】@设置别名_第3张图片

{
  "compilerOptions": {
    "target": "ES2020",
    "useDefineForClassFields": true,
    "module": "ESNext",
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "skipLibCheck": true,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,
    "baseUrl": "./",
    "paths": {
      "@/*":["./src/*"]
    }
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

还是报错,path未找到
在这里插入图片描述
path是node中的工具包,安装一下就行

 npm i path

你可能感兴趣的:(Vue3,1024程序员节,前端)