当前位置:网站首页>在CRA创建的项目中使用@并让其识别@路径并给出路径提示
在CRA创建的项目中使用@并让其识别@路径并给出路径提示
2022-07-27 15:08:00 【Mr_ZCheng】
1.vscode识别@路径并给出路径提示
- 在项目根目录创建
jsconfig.json配置文件 - 在配置文件中添加以下配置
jsconfig.json 中:
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@/*": ["src/*"]
}
}
}经过以上操作,VSCode 会自动读取 jsconfig.json 中的配置,让 vscode 知道 @ 就是 src 目录
2.配置@路径别名,让脚手架工具能识别@
CRA(create-react-app) 将所有工程化配置,都隐藏在了 react-scripts 包中,所以,项目中看不到任何配置信息或者是配置文件。
如果要修改 CRA 的默认配置,有以下几种方案:
- 【推荐】通过第三方库来修改,比如,
@craco/craco - 通过执行
yarn eject命令,释放react-scripts中的所有配置到项目中(注意:该操作不可逆!!!
craco的使用步骤
安装包。
npm i -D @craco/craco- 在项目根目录下,创建配置文件:
craco.config.js。在配置文件中就可以做自定义的修改了。
craco.config.js 中配置路径别名
const path = require('path')
module.exports = {
webpack: {
alias: {
'@': path.join(__dirname, 'src')
}
}
}3.修改 package.json 中的脚本命令
package.json 中:
// 将 start/build/test 三个命令修改为 craco 方式
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "react-scripts eject"
},4.在代码中,就可以通过 @ 来表示 src 目录的绝对路径
5.重启项目,让配置生效
边栏推荐
- 随机数公式Random
- Natural sorting: comparable interface, customized sorting: the difference between comparator interface
- Niuke topic -- symmetric binary tree
- [SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
- 牛客题目——链表的奇偶重排、输出二叉树的右视图、括号生成、字符流中第一个不重复的字符
- Process control statement
- C语言之数组
- 渐变环形进度条
- MPC5744p时钟模块
- 分享一个网上搜不到的「Redis」实现「聊天回合制」的方案
猜你喜欢
随机推荐
两表联查1
牛客题目——链表的奇偶重排、输出二叉树的右视图、括号生成、字符流中第一个不重复的字符
Getting started with nvida CUDA dirverapi
Layoff quarrel, musk: I'm too hard; Mercilessly open source a public opinion acquisition project; Feature engineering is as simple as parameter adjustment?! Nerf boss shouted that he couldn't move; Cu
Passive income: return to the original and safe two ways to earn
移动端基础
Complete steps of JDBC program implementation
C语言之文件操作
This large model sparse training method with high accuracy and low resource consumption has been found by Alibaba cloud scientists! Has been included in IJCAI
C语言之分支循环语句
C语言之操作符
三表联查3
Swift QQ授权登录 坑集
牛客题目——最小的K个数
Niuke topic -- Realizing queues with two stacks, stacks containing min functions, and valid bracket sequences
Three table joint query 3
Day07 operation
Select structure
C语言之数据的储存
LOJ 510 - "libreoj noi round 1" memories outside the north school gate [line segment tree]









