一 開始javascript
1 全局安裝腳手架css
npm install -g create-react-app
這有個坑,就是在window下安裝一直會報錯,報錯信息以下:html
解決辦法:在開始菜單欄裏打開cmd的時,右擊選擇「以管理員身份運行」。而後再在打開的cmd裏運動install就沒問題了。java
2 經過腳手架搭建項目node
create-react-app <項目名稱>
3 開始項目react
cd <項目名>
npm run start
1 package.json 一覽webpack
{ ...... "homepage": ".", "dependencies": { "react": "^16.4.0", "react-dom": "^16.4.0", "react-scripts": "1.1.4" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject" } }
2 可用命令說明:git
http://localhost:3000
訪問項目;"homepage": "."
(上面配置文件已給出), 同時build後的項目須要在服務器下才能訪問;不然打開的將是空白頁面;%PUBLIC_URL%
來指向public目錄路徑;Create React App(如下簡稱 CRA)是建立 React 應用的一個腳手架,它與其餘腳手架不一樣的一個地方就是將一些複雜工具(好比 webpack)的配置封裝了起來,讓使用者不用關心這些工具的具體配置,從而下降了工具的使用難度。可是對於一些熟悉 webpack 的開發者來講,他們可能想對 webpack 配置作一些修改,這個時候應該怎麼辦呢?咱們能夠經過項目eject來進行es6
使用 CRA 建立完項目之後,項目在package.json
裏面提供了這樣一個命令:github
{ ... "scripts": { "eject": "react-scripts eject" }, ... }
執行完這個命令——yarn run eject
後會將封裝在 CRA 中的配置所有反編譯
到當前項目,這樣用戶就能夠徹底取得 webpack 文件的控制權,想怎麼修改就怎麼修改了。
踩坑) 使用create-react-app命令建立一個react項目,運行npm run eject生成配置文件,報了下面的錯:
Remove untracked files, stash or commit any changes, and try again. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! test@0.1.0 eject: `react-scripts eject` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the test@0.1.0 eject script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\lenovo\AppData\Roaming\npm-cache\_logs\2018-11-01T04_03_50_129Z-debug.log
主要問題是腳手架添加.gitgnore文件,可是卻沒有本地倉庫,按照如下順序就能夠正常使用
create-react-app test cd test git init git add . git commit -m 'Saving before ejecting' npm run eject
安裝依賴
npm install less-loader less -dev
經過npm run eject暴露出配置時候,webpack配置文件只有webpack.config.js,
但沒有webpack.config.dev.js和webpack.config.prod.js,查看網上各類解決辦法後,發現是由於create-react-app官方腳手架升級了。
這裏咱們就在webpack.config.js配置less。
方法:
//找到此位置 // style files regexes const cssRegex = /\.css$/; const cssModuleRegex = /\.module\.css$/; const sassRegex = /\.(scss|sass)$/; const sassModuleRegex = /\.module\.(scss|sass)$/; //在此添加以下兩個常量 const lessRegex =/\.less$/; const lessModuleRegex=/\.module\.less$/; // This is the production and development configuration. // It is focused on developer experience, fast rebuilds, and a minimal bundle
//找到此位置 { test: cssRegex, exclude: cssModuleRegex, use: getStyleLoaders({ importLoaders: 1, sourceMap: isEnvProduction && shouldUseSourceMap, }), // Don't consider CSS imports dead code even if the // containing package claims to have no side effects. // Remove this when webpack adds a warning or an error for this. // See https://github.com/webpack/webpack/issues/6571 sideEffects: true, }, // Adds support for CSS Modules (https://github.com/css-modules/css-modules) // using the extension .module.css { test: cssModuleRegex, use: getStyleLoaders({ importLoaders: 1, sourceMap: isEnvProduction && shouldUseSourceMap, modules: true, getLocalIdent: getCSSModuleLocalIdent, }), }, //在這以後仿照上面添加以下代碼 { test: lessRegex, exclude: lessModuleRegex, use: getStyleLoaders({ importLoaders: 2, sourceMap: isEnvProduction && shouldUseSourceMap, }), sideEffects: true, }, { test: lessModuleRegex, use: getStyleLoaders({ importLoaders: 2, modules: true, getLocalIdent: getCSSModuleLocalIdent, sourceMap: isEnvProduction && shouldUseSourceMap, }), },
配置變了以後要從新啓動,否則沒法看到效果。
1 安裝
npm install antd-mobile --save
2 使用
入口頁面 (html 或 模板) 相關設置:
引入 FastClick 而且設置 html
meta
(更多參考 #576)引入 Promise 的 fallback 支持 (部分安卓手機不支持 Promise)
<!DOCTYPE html> <html> <head> <!-- set `maximum-scale` for some compatibility issues --> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" /> <script src="https://as.alipayobjects.com/g/component/fastclick/1.0.6/fastclick.js"></script> <script> if ('addEventListener' in document) { document.addEventListener('DOMContentLoaded', function() { FastClick.attach(document.body); }, false); } if(!window.Promise) { document.writeln('<script src="https://as.alipayobjects.com/g/component/es6-promise/3.2.2/es6-promise.min.js"'+'>'+'<'+'/'+'script>'); } </script> </head> <body></body> </html>
3 按需加載:
使用 babel-plugin-import(推薦)。
npm install babel-plugin-import --save-dev
在package.json中添加以下代碼:
"plugins": [ [ "import", { "libraryName": "antd-mobile", "libraryDirectory": "es", "style": "css" } ] ]
而後只需從 antd-mobile 引入模塊便可,無需單獨引入樣式。
// babel-plugin-import 會幫助你加載 JS 和 CSS import { DatePicker } from 'antd-mobile';
["@babel/plugin-proposal-decorators", {"legacy": true}]
修改成
["transform-decorators-legacy"]
"babel": {"plugins": [ + ["@babel/plugin-proposal-decorators", {"legacy": true}] ] },
... "eslintConfig": { // 默認繼承 腳手架自帶的 eslint 配置 "extends": "react-app", // 在這裏擴展新增配置 "rules":{ // 設置規則,具體看官網用戶指南下的規則文檔 "eqeqeq": "off" } }
參考文檔:http://www.javashuo.com/article/p-qvosdpgq-he.html https://www.jianshu.com/p/fa79cbfa6c6c
https://blog.csdn.net/qq_36709020/article/details/80275602
http://www.javashuo.com/article/p-gqqqnlju-cz.html https://ant.design/docs/react/use-with-create-react-app-cn