摘要:環(huán)境搭建從零開始搭建開發(fā)環(huán)境引入安裝依賴新建修改引入并支持安裝依賴打包時將樣式模塊化,我們可以通過或引入樣式,并且相互不沖突。修改,引入創(chuàng)建使用語法報錯修改引入狀態(tài)管理使用裝飾器語法修改修改源碼
環(huán)境搭建
1.從零開始搭建webpack+react開發(fā)環(huán)境
2.引入Typescript
安裝依賴
npm i -S @types/react @types/react-dom
npm i -D typescript awesome-typescript-loader source-map-loader
新建tsconfig.json
{ "compilerOptions": { "outDir": "./dist/", "sourceMap": true, "noImplicitAny": true, "module": "commonjs", "target": "es5", "jsx": "react" }, "include": [ "./src/**/*" ] }
修改webpack.config.js
// webpack.config.js const path = require("path"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const webpack = require("webpack"); module.exports = { entry: { index:"./src/index.js", }, output: { filename: "bundle.js", path: path.resolve(__dirname, "dist") }, devtool: "source-map", // Add ".ts" and ".tsx" as resolvable extensions. resolve: { extensions: [".ts", ".tsx", ".js", ".jsx"] }, module: { rules: [ { test: /.css$/, use: ["style-loader", "css-loader"] }, { test: /.(png|svg|jpg|gif)$/, use: ["url-loader"] }, { test: /.(woff|woff2|eot|ttf|otf)$/, use: ["url-loader"] }, { test: /.(js|jsx)$/, exclude: /node_modules/, use: { loader: "babel-loader" } }, // All files with a ".ts" or ".tsx" extension will be handled by "awesome-typescript-loader". { test: /.tsx?$/, loader: "awesome-typescript-loader" }, ] }, plugins: [ new HtmlWebpackPlugin({ title: "production", template: "./index.html" }), new webpack.NamedModulesPlugin(), new webpack.HotModuleReplacementPlugin() ], devServer: { contentBase: "./dist", hot: true }, };
3.引入less并支持import less modules
安裝依賴
npm i -D less less-loader
npm i -D typings-for-css-modules-loader
tips: typings-for-css-modules-loader
打包時將樣式模塊化,我們可以通過import或require引入樣式,并且相互不沖突。
//demo.less -> demo.less.d.ts //.demo{color:red;} -> export const demo: string; import * as styles from "demo.less"
修改webpack.config.js
// webpack.config.js const path = require("path"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const webpack = require("webpack"); module.exports = { entry: { index:"./src/index.js", }, output: { filename: "bundle.js", path: path.resolve(__dirname, "dist") }, devtool: "source-map", //add .less resolve: { extensions: [".ts", ".tsx", ".js", ".jsx", ".less", ".css"] }, module: { rules: [ { test: /.css$/, use: ["style-loader", "css-loader"] }, //import less modules,name:demo__demo___hash { test: /.less/, use: [ "style-loader", "typings-for-css-modules-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]&namedExport&camelCase&less!less-loader" ] }, { test: /.(png|svg|jpg|gif)$/, use: ["url-loader"] }, { test: /.(woff|woff2|eot|ttf|otf)$/, use: ["url-loader"] }, { test: /.(js|jsx)$/, exclude: /node_modules/, use: { loader: "babel-loader" } }, { test: /.tsx?$/, loader: "awesome-typescript-loader" }, ] }, plugins: [ new HtmlWebpackPlugin({ title: "production", template: "./index.html" }), new webpack.NamedModulesPlugin(), new webpack.HotModuleReplacementPlugin() ], devServer: { contentBase: "./dist", hot: true }, };
4.引入react-routerv4
npm i -S react-router-dom
創(chuàng)建history
import { createHashHistory } from "history"; export default createHashHistory();
使用
import React from "react"; import ReactDom from "react-dom"; import * as styles from "./index.less"; import history from "./helpers/history"; import {Router, Route, Switch, Redirect, Link} from "react-router-dom"; import Hello from "./router/Hello"; import TodoList from "./router/TodoList"; const PrivateRoute = ({ component: Component , ...rest}) => { return (( )}/> ); } ReactDom.render( , document.getElementById("root") );
- Homes
- TodoList
{/* */}
...ES7語法報錯
npm i -S babel-preset-stage-2
修改.babelrc
{ "presets": ["es2015", "react", "stage-2"], }
5.引入mobx狀態(tài)管理
npm i -S mobx mobx-react
使用裝飾器語法
修改tsconfig.json
"compilerOptions": { "target":"es2017", //fix mobx.d.ts error "experimentalDecorators": true, "allowJs": true }
npm i -D babel-plugin-transform-decorators-legacy
修改.babelrc
{ "presets": ["es2015", "react", "stage-2"], "plugins": ["transform-decorators-legacy"] }源碼
Github
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/95076.html
摘要:不斷更新筆記效果有待進一步完善搭建一個基于的多人功能登錄注冊上傳頭像發(fā)表博文發(fā)表留言參考自前端部分以的腳手架搭起的全家桶后端采用開發(fā)環(huán)境開發(fā)環(huán)境要求以上目錄結(jié)構(gòu)如何運行后端默認(rèn)配置在中請確保本地端口默認(rèn)可用發(fā)布到目錄中默 Full-stack-blog(不斷更新筆記) 效果Demo(有待進一步完善)搭建一個基于Koa2的多人blog功能(登錄注冊上傳頭像,發(fā)表博文,發(fā)表留言)參考自ht...
react-demo(娃哈哈商城) Github地址:https://github.com/Aemple/rea... 歡迎Star 技術(shù)棧 前端
摘要:項目地址下載完項目然后即可基于的項目,主要是為了學(xué)習(xí)實戰(zhàn)。數(shù)據(jù)都是固定的,從餓了么接口臨時抓的,模擬了一個的異步數(shù)據(jù)延遲,感謝餓了么。詳細信息可以看上面的官方文檔,我這里就簡單說一下我這個項目的應(yīng)用。 react-ele-webapp 項目地址 :https://github.com/kliuj/reac... run 下載完項目npm install然后npm run dev 即可 ...
摘要:解決思路服務(wù)器端渲染服務(wù)器端和前端公用同一個應(yīng)用,然后通過構(gòu)建工具及配置,確定哪些組件需要再服務(wù)器端渲染,那些組件需要再客戶端渲染。服務(wù)器端渲染,由框架與構(gòu)建工具配合,并依據(jù)一定的項目結(jié)構(gòu)和編碼方式,共同運行。 分離 為什么需要 前后端分離、web服務(wù)器與static服務(wù)器分離: 前端與后端耦合 (需求) 自動化、工程化的構(gòu)建前端的代碼 (基礎(chǔ)條件) 模塊化、組件化,項目共享代碼 (...
摘要:基于全家桶寫作新聞一體綜合應(yīng)用的實踐總結(jié)在線地址大家伙兒們,又見面了。參照但不可否認(rèn)非常符合的思想,都在處理大規(guī)模數(shù)據(jù)時彰顯優(yōu)勢。最好的辦法是使用部署環(huán)境。細致的拆分,解耦性更好,以為單位進行修改時,大大降低誤傷率的同時,隔離無關(guān)的信息。 ?CoderPad-基于React全家桶寫作/新聞一體綜合應(yīng)用的實踐總結(jié) showImg(https://segmentfault.com/img/...
閱讀 2648·2021-11-24 09:39
閱讀 1648·2021-11-24 09:38
閱讀 629·2021-11-22 14:44
閱讀 1888·2021-11-18 10:02
閱讀 2573·2021-11-18 10:02
閱讀 1158·2021-10-14 09:43
閱讀 4244·2021-09-29 09:35
閱讀 523·2021-07-30 15:30