使用 Create React App 脚手架来快速创建react项目
//全局安装create-react-app
npm install -g create-react-app
//projectName为自定义项目名字
create-react-app projectName
//进入项目
cd projectName
//运行项目
npm start / yarn start
在浏览器中打开http://localhost:3000查看。
配置绝对路径:
在项目根目录(与src同级)创建 jsconfig.json文件 写入:
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
例如项目结构:
src
├─ components
├─├─ Home.js
├─comment
引入Home.js 或Home.jsx 直接写:
import Home from 'components/Home ';
//此处有坑:
//如果省略 .jsx 报错的话 重新项目就好了
安装scss
yarn add node-sass / npm install node-sass -D
(我这里安装之后报错 让重新执行一次npm install)
引入scss时候报错 :
Error:Node Sass version 5.0.0 is incompatible with ^4.0.0
意思是版本不匹配
解决:卸载node-sass
npm uninstall node-sass / yarn remove node-sass --dev
然后安装最新版本(5.0之前)
npm install node-sass@4.14.1 / yarn add node-sass@4.14.1
(太慢了我凑 装了半个小时···)
最后运行一下 终于好了
npm rebuild node-sass
答案呢