React项目配置路径别名“@”

React项目配置路径别名"@"

首先安装craco

bash 复制代码
npm i @craco/craco@alpha -D
npm i npm i craco-less

创建craco.config.js

bash 复制代码
const path = require('path')
const CracoLessPlugin = require('craco-less')

const resolve = (dir) => path.resolve(__dirname, dir)

module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            // modifyVars: { '@primary-color': '#1DA57A' },
            javascriptEnabled: true
          }
        }
      }
    }
  ],
  webpack: {
    alias: {
      '@': resolve('src'),
      components: resolve('src/components')
    }
  }
}

修改tsconfig.json

bash 复制代码
{
  "compilerOptions": {
    .......加上如下两个配置(删掉这个)
    "baseUrl": "src",
    "paths": {
      "@/*": ["*"]
    }
  },
  "include": [
    "src"
  ]
}

修改package.json 使用craco启动项目

bash 复制代码
 "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "react-scripts eject"
  },

最后启动成功后可能会出现问题 提示你 babel-preset-react-app 未在其依赖中声明

你需要继续安装

bash 复制代码
npm install --save-dev @babel/plugin-proposal-private-property-in-object
相关推荐
程序视点1 小时前
Escrcpy 3.0投屏控制软件使用教程:无线/有线连接+虚拟显示功能详解
前端·后端
silent_missile1 小时前
element-plus穿梭框transfer的调整
前端·javascript·vue.js
专注VB编程开发20年1 小时前
OpenXml、NPOI、EPPlus、Spire.Office组件对EXCEL ole对象附件的支持
前端·.net·excel·spire.office·npoi·openxml·spire.excel
古蓬莱掌管玉米的神1 小时前
coze娱乐ai换脸
前端
GIS之路2 小时前
GeoTools 开发合集(全)
前端
咖啡の猫2 小时前
Shell脚本-嵌套循环应用案例
前端·chrome
一点一木2 小时前
使用现代 <img> 元素实现完美图片效果(2025 深度实战版)
前端·css·html
萌萌哒草头将军3 小时前
🚀🚀🚀 告别复制粘贴,这个高效的 Vite 插件让我摸鱼🐟时间更充足了!
前端·vite·trae
布列瑟农的星空3 小时前
大话设计模式——关注点分离原则下的事件处理
前端·后端·架构
yvvvy3 小时前
前端必懂的 Cache 缓存机制详解
前端