再看本篇文章之前,本人还是建议想入坑react的童鞋可以选有create-react-app来创建react的项目,因为现在dva和roadhog还不成熟,坑相对要多一些,当然如果你已经做好跳坑的准备,那么请继续往下走;

本文适合对 ES6+webpack 有一定了解的人。没有的了解的同学可以先看看下面的我分享的链接,

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的项目

第一步

  1. 安装 dva roadhog
  2. npm i dva-cli roadhog -g
  3. 好啦~现在你已经学会了怎么安装dvaroadhog啦,接下来就可以创建项目啦

第二步

  1. 创建项目
  2. dva new projectName
  3. npm install
  4. npm start
  5. 打开浏览器输入localhost:8000,看到欢迎界面证明第二步已经成功啦

第三步

  1. 添加配置文件和安装webpack
  2. 安装 lodash babel-plugin webpack-plugin shim 并添加到package.json文件中
  3. npm install --save-dev webpack 安装本地webpack配置文件
  4. webpack 文件
  5. // webpack配置
  6. import glob from 'glob';
  7. import webpack from 'webpack';
  8. import { isRegExp } from 'lodash';
  9. import pxtorem from 'postcss-pxtorem';
  10. import HtmlWebpackPlugin from 'html-webpack-plugin';
  11. import ExtractTextPlugin from 'extract-text-webpack-plugin';
  12. import LodashModuleReplacementPlugin from 'lodash-webpack-plugin';
  13. const path = require('path');
  14. export default ( webpackConfig, env ) => {
  15. const loaders = webpackConfig.module.loaders;
  16. const postcss = webpackConfig.postcss;
  17. webpackConfig.postcss = function () {
  18. const postcssArray = postcss();
  19. postcssArray.push( pxtorem( {
  20. rootValue: 100,
  21. propWhiteList: []
  22. } ) );
  23. return postcssArray;
  24. };
  25. const svgDirs = [
  26. require.resolve( 'antd-mobile' ).replace( /warn\.js$/, '' ), // antd-mobile 内置svg // 引入antd-mobile
  27. path.resolve(__dirname, 'src/assets/icon'),
  28. ];
  29. loaders.forEach( ( loader ) => {
  30. if ( loader.test && loader.test.toString() === '/\\.svg$/' ) {
  31. loader.exclude = svgDirs;
  32. }
  33. } );
  34. loaders.unshift( {
  35. test: /\.svg$/,
  36. loader: 'svg-sprite',
  37. include: svgDirs
  38. } );
  39. const noParse = webpackConfig.module.noParse;
  40. if ( Array.isArray( noParse ) ) {
  41. noParse.push( /moment.js/ );
  42. }
  43. else if ( noParse ) {
  44. webpackConfig.module.noParse = [ noParse, /moment.js/ ];
  45. }
  46. else {
  47. webpackConfig.module.noParse = [ /moment.js/ ];
  48. }
  49. // lodash
  50. webpackConfig.babel.plugins.push( 'lodash' );
  51. webpackConfig.plugins.push( new LodashModuleReplacementPlugin() );
  52. loaders.push( {
  53. test: /\.(png|jpg|jpeg|gif)(\?v=\d+\.\d+\.\d+)?$/i,
  54. loader: 'file'
  55. } );
  56. // 打包配置
  57. if ( env === 'production' ) {
  58. //添加hash
  59. webpackConfig.output.filename = '[name].[chunkhash:6].js';
  60. webpackConfig.output.chunkFilename = '[name].[chunkhash:6].js';
  61. webpackConfig.plugins.forEach( ( plugin, index, plugins ) => {
  62. if ( plugin instanceof ExtractTextPlugin ) {
  63. plugins[ index ] = new ExtractTextPlugin( '[name].[chunkhash:6].css', {
  64. disable: false,
  65. allChunks: true
  66. } );
  67. }
  68. else if ( plugin instanceof webpack.optimize.CommonsChunkPlugin ) {
  69. plugins[ index ] = new webpack.optimize.CommonsChunkPlugin(
  70. 'common',
  71. 'common.[chunkhash:6].js'
  72. );
  73. }
  74. } );
  75. }
  76. //HTML
  77. webpackConfig.module.loaders = loaders.filter(
  78. loader => isRegExp( loader.test ) && loader.test.toString() !== '/\\.html$/'
  79. );
  80. webpackConfig.plugins.push(
  81. new HtmlWebpackPlugin( {
  82. // favicon: './src/logo/logo.ico',
  83. template: './src/index.html',
  84. filename: 'index.html',
  85. inject: true
  86. } )
  87. );
  88. return webpackConfig;
  89. };

到现在你已经完成了一半啦 是不是觉得很简单。对啦 这里有一点要注意,复制 es5-shim.min.js es5-sham.min.js console-polyfill/index.js 文件到 public 文件夹console-polyfill/index.js 改名为 console-polyfill.js

第四步 roadhog、proxy配置和antd-mobile引入

  1. 废话不说 这步直接上代码(对应的是目录中的.roadhogrc.js,大学按步骤下来的话这应该是.roadhogrc.json的文件,但是本人还是比较喜欢js语法,所以做了修改,此处因人而异)
  2. import path from 'path';
  3. export default {
  4. '/api': {
  5. target:'localhost',//这里是你的接口地址,我随便写的
  6. changeOrigin: true
  7. },
  8. multipage: true,
  9. theme: 'antd.config.js',
  10. entry: [ 'src/common.js', 'src/index.js' ],
  11. env: { //下面是在开发环境和生产环境都引入antd-mobile
  12. development: {
  13. extraBabelPlugins: [
  14. 'dva-hmr',
  15. 'transform-runtime',
  16. [ 'import', { libraryName: 'antd-mobile', style: true }]
  17. ]
  18. },
  19. production: {
  20. extraBabelPlugins: [
  21. 'transform-runtime',
  22. [ 'import', { libraryName: 'antd-mobile', style: true }]
  23. ]
  24. }
  25. }
  26. };

好啦,以上四步差不多就可以用dva把react的项目架子搭建起来,再有就是eslint的配置啦,此处不做讲解(http://eslint.org/docs/user-g...),接下来你可以在src中尝试着运行一下Hello World啦

还有一个点需要注意的是,dva 建项目的时候会默认安装redux和react-router,所以在开发中千万不要在去安装,会因为版本不兼容而导致项目无法运行;

最后给大家分享一些用到的资料
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/...

项目地址:https://github.com/tengwei30/...

更多精彩敬请期待。。。

React+dva+webpack+antd-mobile 实战分享(一)的更多相关文章

  1. React+dva+webpack+antd-mobile 实战分享(二)

    第一篇 https://segmentfault.com/a/11... 在上一篇文章中教给大家了怎么搭建项目的架子:那么今天我们就来说一下项目里的导航和列表的实现 导航 废话不说啦 下面直接给大家讲 ...

  2. [书籍精读]《React Native精解与实战》精读笔记分享

    写在前面 书籍介绍:本书由架构师撰写,包含ReactNative框架底层原理,以及与iOS.Android混合开发案例,精选了大量实例代码,方便读者快速学习.主要内容分为两大部分,第1部分" ...

  3. 《React Native 精解与实战》书籍连载「React 与 React Native 简介」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  4. React配合Webpack实现代码分割与异步加载

    这是Webpack+React系列配置过程记录的第四篇.其他内容请参考: 第一篇:使用webpack.babel.react.antdesign配置单页面应用开发环境 第二篇:使用react-rout ...

  5. 《React Native 精解与实战》书籍连载「Android 平台与 React Native 混合开发」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  6. 《React Native 精解与实战》书籍连载「iOS 平台与 React Native 混合开发」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  7. 《React Native 精解与实战》书籍连载「配置 iOS 与 Android 开发环境」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  8. 《React Native 精解与实战》书籍连载「React Native 网络请求与列表绑定」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  9. 《React Native 精解与实战》书籍连载「React Native 中的生命周期」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

随机推荐

  1. 将ISO镜像文件制作成USB disk

    制作USB Live盘有Universal USB Installer.UNetbootin.WinSetupFromUSB.LinuxLive USB Creator.YUMI(Your Unive ...

  2. 组合拳 | 本地文件包含漏洞+TFTP=Getshell

    文章声明 安全文章技术仅供参考,此文所提供的信息为漏洞靶场进行渗透,未经授权请勿利用文章中的技术资料对任何计算机系统进行入侵操作. 本文所提供的工具仅用于学习,禁止用于其他,未经授权,严禁转载,如需转 ...

  3. redirect route 路由传参

    return redirect()->route('exams.sign',['token'=>$token,'id'=>$result['id']]); // 签到页面 Route ...

  4. xshell + xftp 安装及1603错误解决

    xshell + xftpan下载安装 百度网盘下载链接:https://pan.baidu.com/s/14orvEWDjFkrLvr_9JaG4Gw 提取码:om9z 下载地址 https://w ...

  5. 通过IMM With Remote Console为服务器安装操作系统

    现有IBM System x3750 M4(8722A1C)配置   产品类别 机架式 产品结构 2U CPU类型 Intel 至强E5-4600 CPU型号 Xeon E5-4640 CPU频率 2 ...

  6. yum安装zabbix-web-mysql出现[Errno 256] No more mirrors to try.

    yum安装zabbix-web-mysql出现[Errno 256] No more mirrors to try.报错在CentOS7.X 使用yum 安装软件的时候 出现错误[Errno 256] ...

  7. 2.2 C++STL string容器详解

    文章目录 引言 2.2.1 string的特性 2.2.2 string用法理论 2.2.2.1 string构造函数 2.2.2.2 string赋值操作 2.2.2.3 string取值操作 2. ...

  8. Windows原理深入学习系列-信任等级检查

    这是[信安成长计划]的第 23 篇文章 0x00 目录 0x01 介绍 0x02 逆向分析 Win10_x64_20H2 0x03 WinDBG 0x04 参考文章 在之前的时候,一直以为 SACL ...

  9. Linux kernel cfg80211_mgd_wext_giwessid缓冲区溢出漏洞

    受影响系统:Linux kernel <= 5.3.2描述:CVE(CAN) ID: CVE-2019-17133 Linux kernel是开源操作系统Linux所使用的内核. Linux k ...

  10. SqlServer Split 的实现

    数据库如何处理传参用指定字符隔开参数的情况 如"name1,name3,name5" 共2种方式, 1.数据库内置函数STRING_SPLIT(sql2016之前的版本不支持该函数 ...