亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

React+dva+webpack+antd-mobile 實(shí)戰(zhàn)分享(一)

ziwenxie / 894人閱讀

摘要:再看本篇文章之前,本人還是建議想入坑的童鞋可以選有來創(chuàng)建的項(xiàng)目,因?yàn)楝F(xiàn)在和還不成熟,坑相對(duì)要多一些,當(dāng)然如果你已經(jīng)做好跳坑的準(zhǔn)備,那么請(qǐng)繼續(xù)往下走本文適合對(duì)有一定了解的人。

再看本篇文章之前,本人還是建議想入坑react的童鞋可以選有create-react-app來創(chuàng)建react的項(xiàng)目,因?yàn)楝F(xiàn)在dva和roadhog還不成熟,坑相對(duì)要多一些,當(dāng)然如果你已經(jīng)做好跳坑的準(zhǔn)備,那么請(qǐng)繼續(xù)往下走;
本文適合對(duì) ES6+webpack 有一定了解的人。沒有的了解的同學(xué)可以先看看下面的我分享的鏈接,

ES6: http://www.jianshu.com/p/ebfe...
Webpack: https://doc.webpack-china.org...
react: https://facebook.github.io/re...
antd-mobile:https://mobile.ant.design/doc...

扯完啦,接下來就是正題啦,先看效果

今天主要是想給大家說一下怎么用dva來搭建react的項(xiàng)目

第一步
安裝 dva 和 roadhog;
    npm i dva-cli roadhog -g 
好啦~現(xiàn)在你已經(jīng)學(xué)會(huì)了怎么安裝dva和roadhog啦,接下來就可以創(chuàng)建項(xiàng)目啦
第二步
創(chuàng)建項(xiàng)目
dva new projectName
npm install
npm start

打開瀏覽器輸入localhost:8000,看到歡迎界面證明第二步已經(jīng)成功啦

第三步
添加配置文件和安裝webpack

安裝 lodash babel-plugin webpack-plugin shim 并添加到package.json文件中

npm install --save-dev webpack 安裝本地webpack配置文件

webpack 文件
    // webpack配置
    import glob from "glob";
    import webpack from "webpack";
    import { isRegExp } from "lodash";
    import pxtorem from "postcss-pxtorem";
    import HtmlWebpackPlugin from "html-webpack-plugin";
    import ExtractTextPlugin from "extract-text-webpack-plugin";
    import LodashModuleReplacementPlugin from "lodash-webpack-plugin";
    
    
    const path = require("path");
    export default ( webpackConfig, env ) => {
    
      const loaders = webpackConfig.module.loaders;
      const postcss = webpackConfig.postcss;
      webpackConfig.postcss = function () {
        const postcssArray = postcss();
        postcssArray.push( pxtorem( {
          rootValue: 100,
          propWhiteList: []
        } ) );
        return postcssArray;
      };
      const svgDirs = [
        require.resolve( "antd-mobile" ).replace( /warn.js$/, "" ), // antd-mobile 內(nèi)置svg    // 引入antd-mobile
        path.resolve(__dirname, "src/assets/icon"),
      ];
    
      loaders.forEach( ( loader ) => {
        if ( loader.test && loader.test.toString() === "/.svg$/" ) {
          loader.exclude = svgDirs;
        }
      } );
    
      loaders.unshift( {
        test: /.svg$/,
        loader: "svg-sprite",
        include: svgDirs
      } );
      const noParse = webpackConfig.module.noParse;
      if ( Array.isArray( noParse ) ) {
        noParse.push( /moment.js/ );
      }
      else if ( noParse ) {
        webpackConfig.module.noParse = [ noParse, /moment.js/ ];
      }
      else {
        webpackConfig.module.noParse = [ /moment.js/ ];
      }
    
      // lodash
      webpackConfig.babel.plugins.push( "lodash" );
      webpackConfig.plugins.push( new LodashModuleReplacementPlugin() );
    
      loaders.push( {
        test: /.(png|jpg|jpeg|gif)(?v=d+.d+.d+)?$/i,
        loader: "file"
      } );
    
      // 打包配置
      if ( env === "production" ) {            
         //添加hash
        webpackConfig.output.filename = "[name].[chunkhash:6].js";
        webpackConfig.output.chunkFilename = "[name].[chunkhash:6].js";
    
        webpackConfig.plugins.forEach( ( plugin, index, plugins ) => {
          if ( plugin instanceof ExtractTextPlugin ) {
            plugins[ index ] = new ExtractTextPlugin( "[name].[chunkhash:6].css", {
              disable: false,
              allChunks: true
            } );
          }
          else if ( plugin instanceof webpack.optimize.CommonsChunkPlugin ) {
            plugins[ index ] = new webpack.optimize.CommonsChunkPlugin(
                "common",
                "common.[chunkhash:6].js"
            );
          }
        } );
    
      }
      //HTML
      webpackConfig.module.loaders = loaders.filter(
              loader => isRegExp( loader.test ) && loader.test.toString() !== "/.html$/"
      );
      webpackConfig.plugins.push(
          new HtmlWebpackPlugin( {
            // favicon: "./src/logo/logo.ico",
            template: "./src/index.html",
            filename: "index.html",
            inject: true
          } )
      );
    
      return webpackConfig;
    };

到現(xiàn)在你已經(jīng)完成了一半啦 是不是覺得很簡(jiǎn)單。對(duì)啦 這里有一點(diǎn)要注意,復(fù)制 es5-shim.min.js es5-sham.min.js console-polyfill/index.js 文件到 public 文件夾console-polyfill/index.js 改名為 console-polyfill.js

第四步 roadhog、proxy配置和antd-mobile引入
廢話不說 這步直接上代碼(對(duì)應(yīng)的是目錄中的.roadhogrc.js,大學(xué)按步驟下來的話這應(yīng)該是.roadhogrc.json的文件,但是本人還是比較喜歡js語法,所以做了修改,此處因人而異)
     import path from "path";
    
    export default {
     "/api": {
        target:"localhost",//這里是你的接口地址,我隨便寫的
        changeOrigin: true
      },
      multipage: true,
      theme: "antd.config.js",
      entry: [ "src/common.js", "src/index.js" ],
      env: { //下面是在開發(fā)環(huán)境和生產(chǎn)環(huán)境都引入antd-mobile
        development: {
          extraBabelPlugins: [
            "dva-hmr",
            "transform-runtime",
            [ "import", { libraryName: "antd-mobile", style: true }]
          ]
        },
        production: {
          extraBabelPlugins: [
            "transform-runtime",
            [ "import", { libraryName: "antd-mobile", style: true }]
          ]
        }
      }
    };
    

好啦,以上四步差不多就可以用dva把react的項(xiàng)目架子搭建起來,再有就是eslint的配置啦,此處不做講解(http://eslint.org/docs/user-g...),接下來你可以在src中嘗試著運(yùn)行一下Hello World啦

還有一個(gè)點(diǎn)需要注意的是,dva 建項(xiàng)目的時(shí)候會(huì)默認(rèn)安裝redux和react-router,所以在開發(fā)中千萬不要在去安裝,會(huì)因?yàn)榘姹静患嫒荻鴮?dǎo)致項(xiàng)目無法運(yùn)行;

最后給大家分享一些用到的資料
antd主題制定: https://ant.design/docs/react...
roadhog: https://github.com/sorrycc/ro...
webpack中proxy配置: https://webpack.github.io/doc...
redux: http://www.redux.org.cn/
react-router: http://react-guide.github.io/...

項(xiàng)目地址:https://github.com/tengwei30/...

更多精彩敬請(qǐng)期待。。。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

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

相關(guān)文章

  • React+dva+webpack+antd-mobile 實(shí)戰(zhàn)分享(二)

    摘要:第一篇在上一篇文章中教給大家了怎么搭建項(xiàng)目的架子那么今天我們就來說一下項(xiàng)目里的導(dǎo)航和列表的實(shí)現(xiàn)導(dǎo)航廢話不說啦下面直接給大家講一下代碼項(xiàng)目用的的框架應(yīng)該沒什么難度,我相信大家認(rèn)真看文檔的都能布局出來這個(gè)地方是封裝的請(qǐng)求請(qǐng)求過來的數(shù)據(jù)全部存 第一篇 https://segmentfault.com/a/11... 在上一篇文章中教給大家了怎么搭建項(xiàng)目的架子;那么今天我們就來說一下項(xiàng)目里的導(dǎo)...

    eternalshallow 評(píng)論0 收藏0
  • 2017-07-01 前端日?qǐng)?bào)

    摘要:前端日?qǐng)?bào)精選騰訊前端團(tuán)隊(duì)社區(qū)源碼分析入門指南一些關(guān)于使用的心得基本類型與引用類型知多少掘金中文第期框架選型周刊第期入門系列模塊車棧重構(gòu)基于的網(wǎng)絡(luò)請(qǐng)求庫某熊的全棧之路的那些奇技淫巧的平凡之路模仿寫個(gè)數(shù)組監(jiān)聽掘 2017-07-01 前端日?qǐng)?bào) 精選 Why you shouldn`t use Preact, Fast-React, etc. to replace React today -...

    _DangJin 評(píng)論0 收藏0
  • 使用Dva+antd-mobile構(gòu)建個(gè)移動(dòng)端web

    摘要:整個(gè)應(yīng)用的,由多個(gè)小的的以為合成該當(dāng)前的狀態(tài)。執(zhí)行異步函數(shù)發(fā)出一個(gè),類似于取的值通過函數(shù)將需要用到的數(shù)據(jù)合并到中,再在組件中取修改的值通過被的會(huì)自動(dòng)在中擁有方法請(qǐng)求統(tǒng)一講用到所有的接口放到中方便管理函數(shù)柯里化 項(xiàng)目地址:dva-demo 隨手一個(gè)小star給予動(dòng)力,謝謝! Build Setup # install dependencies npm install # serve ...

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

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

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<