再看本篇文章之前,本人还是建议想入坑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的项目

第一步

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

第二步

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

第三步

添加配置文件和安装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 内置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;
};

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

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

废话不说 这步直接上代码(对应的是目录中的.roadhogrc.js,大学按步骤下来的话这应该是.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: { //下面是在开发环境和生产环境都引入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的项目架子搭建起来,再有就是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/...

更多精彩敬请期待。。。

最新文章

  1. vue的选项
  2. SQL Server 存储(4/8):理解Page Free Space (PFS) 页
  3. windows 下安装Yii2 高级版本
  4. 【转】Kali Linux 新手折腾笔记
  5. find-right-interval
  6. C语言 猜数游戏--产生一个随机数
  7. 15、自定义Content Provider
  8. 【C# C++】C#中调用msvcr100.dll中的_beginthreadex函数
  9. C# 之 SqlConnection 类
  10. VS2010 快速写入注释小技巧
  11. C++ lambda 表达式传递的变量默认不可变
  12. haskell学习笔记<1>--基本语法
  13. python机器学习模块安装
  14. Selenium2Lib库之输入常用关键字实战
  15. Python 带参数的装饰器 [2] 函数参数类型检查
  16. WiFi-ESP8266入门http(3-2)网页认证上网-post请求
  17. linux环境中,如何解压后缀是bz2的压缩包?tar.bz2格式的压缩包,如何进行解压?
  18. Java中String类两种实例化的区别(转)
  19. 查阅JDK,collection与collections区别大
  20. 3: $.ajax()方法详解

热门文章

  1. LeetCode-092-反转链表 II
  2. SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)
  3. C#-使用HttpListener创建http服务
  4. centos7下mysql安装
  5. MySQL 8.0无法通过Navicat远程连接 1045 - Access denied for user 'root'@'::1' (using password: YES) 的解决办法
  6. Swagger整合Jwt授权配置
  7. LGP7847题解
  8. VuePress 博客优化之增加 Vssue 评论功能
  9. TypeScript-axios模块进行封装的操作与一些想法
  10. wordcloud库 词云