成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

React全家桶環(huán)境搭建過程

Genng / 2693人閱讀

摘要:環(huán)境搭建從零開始搭建開發(fā)環(huán)境引入安裝依賴新建修改引入并支持安裝依賴打包時(shí)將樣式模塊化,我們可以通過或引入樣式,并且相互不沖突。修改,引入創(chuàng)建使用語(yǔ)法報(bào)錯(cuò)修改引入狀態(tài)管理使用裝飾器語(yǔ)法修改修改源碼

環(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

打包時(shí)將樣式模塊化,我們可以通過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(
    
        
  • Homes
  • TodoList
{/**/}
, document.getElementById("root") );

...ES7語(yǔ)法報(bào)錯(cuò)

npm i -S babel-preset-stage-2

修改.babelrc

{
  "presets": ["es2015", "react", "stage-2"],
}

5.引入mobx狀態(tài)管理

npm i -S mobx mobx-react

使用裝飾器語(yǔ)法

修改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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/95076.html

相關(guān)文章

  • react全家+koa2實(shí)現(xiàn)一個(gè)多人博客~

    摘要:不斷更新筆記效果有待進(jìn)一步完善搭建一個(gè)基于的多人功能登錄注冊(cè)上傳頭像發(fā)表博文發(fā)表留言參考自前端部分以的腳手架搭起的全家桶后端采用開發(fā)環(huán)境開發(fā)環(huán)境要求以上目錄結(jié)構(gòu)如何運(yùn)行后端默認(rèn)配置在中請(qǐng)確保本地端口默認(rèn)可用發(fā)布到目錄中默 Full-stack-blog(不斷更新筆記) 效果Demo(有待進(jìn)一步完善)搭建一個(gè)基于Koa2的多人blog功能(登錄注冊(cè)上傳頭像,發(fā)表博文,發(fā)表留言)參考自ht...

    mdluo 評(píng)論0 收藏0
  • 一個(gè)還不錯(cuò)的 React 全家 Demo(后續(xù)發(fā)詳細(xì)搭建步驟)

    react-demo(娃哈哈商城) Github地址:https://github.com/Aemple/rea... 歡迎Star 技術(shù)棧 前端

    leone 評(píng)論0 收藏0
  • react+react-router+react-redux全家小項(xiàng)目開發(fā)過程分享

    摘要:項(xiàng)目地址下載完項(xiàng)目然后即可基于的項(xiàng)目,主要是為了學(xué)習(xí)實(shí)戰(zhàn)。數(shù)據(jù)都是固定的,從餓了么接口臨時(shí)抓的,模擬了一個(gè)的異步數(shù)據(jù)延遲,感謝餓了么。詳細(xì)信息可以看上面的官方文檔,我這里就簡(jiǎn)單說一下我這個(gè)項(xiàng)目的應(yīng)用。 react-ele-webapp 項(xiàng)目地址 :https://github.com/kliuj/reac... run 下載完項(xiàng)目npm install然后npm run dev 即可 ...

    zzir 評(píng)論0 收藏0
  • 構(gòu)建前端項(xiàng)目

    摘要:解決思路服務(wù)器端渲染服務(wù)器端和前端公用同一個(gè)應(yīng)用,然后通過構(gòu)建工具及配置,確定哪些組件需要再服務(wù)器端渲染,那些組件需要再客戶端渲染。服務(wù)器端渲染,由框架與構(gòu)建工具配合,并依據(jù)一定的項(xiàng)目結(jié)構(gòu)和編碼方式,共同運(yùn)行。 分離 為什么需要 前后端分離、web服務(wù)器與static服務(wù)器分離: 前端與后端耦合 (需求) 自動(dòng)化、工程化的構(gòu)建前端的代碼 (基礎(chǔ)條件) 模塊化、組件化,項(xiàng)目共享代碼 (...

    mindwind 評(píng)論0 收藏0
  • CoderPad-基于React全家寫作/新聞一體綜合應(yīng)用的實(shí)踐總結(jié)

    摘要:基于全家桶寫作新聞一體綜合應(yīng)用的實(shí)踐總結(jié)在線地址大家伙兒們,又見面了。參照但不可否認(rèn)非常符合的思想,都在處理大規(guī)模數(shù)據(jù)時(shí)彰顯優(yōu)勢(shì)。最好的辦法是使用部署環(huán)境。細(xì)致的拆分,解耦性更好,以為單位進(jìn)行修改時(shí),大大降低誤傷率的同時(shí),隔離無(wú)關(guān)的信息。 ?CoderPad-基于React全家桶寫作/新聞一體綜合應(yīng)用的實(shí)踐總結(jié) showImg(https://segmentfault.com/img/...

    DC_er 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<