结构

src/utils/utils.less#

可以放置一些工具函数供调用,比如清除浮动.clearfix等。

  1. .clearfix() {
  2. zoom: 1;
  3. &::before,
  4. &::after {
  5. content: ' ';
  6. display: table;}

utils/request.js

utils/request.js是基于 fetch 的封装,便于统一处理 POST,GET 等请求参数,请求头,以及错误提示信息等

package.json

  1. "scripts": {
  2. ...
  3. "start": "cross-env APP_TYPE=site umi dev"}

APP_TYPE=site:让APP_TYPE=site是对umi 的一个脚手架,之后执行umi

config.js

默认/基本配置

route.config.js

项目的整体流程在route.config中有完整的体现

脚手架

软件开发的脚手架的概念是从建筑学术语上引申而来。在建筑学上,脚手架指施工现场为工人操作并解决垂直和水平运输而搭设的各种支架。大楼施工过程中在大楼外墙围的那圈支架就是其中一种脚手架。
在软件开发上(当然也包括前端开发)脚手架指的就是:有人帮你把这个开发过程中要用到的工具、环境都配置好了,可以方便地直接开始做开发,专注你的业务,而不用再花时间去配置这个开发环境,开发环境就是脚手架。
比如vue.js就有个vue-cli脚手架,基于node.js的开发环境,作者帮你把开发环境大部分东西都配置好了,你把脚手架下载下来就可以直接开发了,不用再考虑搭建这些工具环境。

map映射

[].map(); 基本用法跟forEach方法类似:

  1. array.map(callback,[ thisObject]);
  2. //callback的参数也类似:
  3. [].map(function(value, index, array) {
  4. // ...
  5. });
  6. //map方法的作用不难理解,“映射”嘛,也就是原数组被“映射”成对应新数组。下面这个例子是数值项求平方:
  7. var data = [1, 2, 3, 4];
  8. var arrayOfSquares = data.map(function (item) {
  9. return item * item;
  10. });
  11. alert(arrayOfSquares); // 1, 4, 9, 16
  12. //callback需要有return值,如果没有,就像下面这样:
  13. var data = [1, 2, 3, 4];
  14. var arrayOfSquares = data.map(function() {});
  15. arrayOfSquares.forEach(console.log);
  16. //结果如下图,可以看到,数组所有项都被映射成了undefined:
  17. ///全部项都成了undefined
  18. //在实际使用的时候,我们可以利用map方法方便获得对象数组中的特定属性值们。
  19. //例如下面这个例子(之后的兼容demo也是该例子):
  20. var users = [
  21. {name: "张含韵", "email": "zhang@email.com"},
  22. {name: "江一燕", "email": "jiang@email.com"},
  23. {name: "李小璐", "email": "li@email.com"}
  24. ];
  25. var emails = users.map(function (user) { return user.email; });
  26. console.log(emails.join(", ")); // zhang@email.com, jiang@email.com, li@email.com
  27. Array.prototype扩展可以让IE6-IE8浏览器也支持map方法:
  28. if (typeof Array.prototype.map != "function") {
  29. Array.prototype.map = function (fn, context) {
  30. var arr = [];
  31. if (typeof fn === "function") {
  32. for (var k = 0, length = this.length; k < length; k++) {
  33. arr.push(fn.call(context, this[k], k, this));
  34. }
  35. }
  36. return arr;
  37. };
  38. }
  1. export default {
  2. UserName: {
  3. props: {
  4. size: 'large',
  5. id: 'userName',
  6. prefix: <Icon type="user" className={styles.prefixIcon} />,
  7. placeholder: 'admin',
  8. },
  9. rules: [
  10. {
  11. required: true,
  12. message: 'Please enter username!',
  13. },
  14. ],
  15. },
  16. Password: {
  17. props: {
  18. size: 'large',
  19. prefix: <Icon type="lock" className={styles.prefixIcon} />,
  20. type: 'password',
  21. id: 'password',
  22. placeholder: '888888',
  23. },
  24. rules: [
  25. {
  26. required: true,
  27. message: 'Please enter password!',
  28. },
  29. ],
  30. },
  31. Mobile: {
  32. props: {
  33. size: 'large',
  34. prefix: <Icon type="mobile" className={styles.prefixIcon} />,
  35. placeholder: 'mobile number',
  36. },
  37. rules: [
  38. {
  39. required: true,
  40. message: 'Please enter mobile number!',
  41. },
  42. {
  43. pattern: /^1\d{10}$/,
  44. message: 'Wrong mobile number format!',
  45. },
  46. ],
  47. },
  48. Captcha: {
  49. props: {
  50. size: 'large',
  51. prefix: <Icon type="mail" className={styles.prefixIcon} />,
  52. placeholder: 'captcha',
  53. },
  54. rules: [
  55. {
  56. required: true,
  57. message: 'Please enter Captcha!',
  58. },
  59. ],
  60. },
  61. };

BasicLayout

布局-侧边栏、头部、内容、底部

  1. import React, { Suspense } from 'react';
  2. import { Layout } from 'antd';
  3. import DocumentTitle from 'react-document-title';
  4. import { connect } from 'dva';
  5. import { ContainerQuery } from 'react-container-query';
  6. import classNames from 'classnames';
  7. import Media from 'react-media';
  8. import logo from '../assets/logo.svg';
  9. import Footer from './Footer';
  10. import Header from './Header';
  11. import Context from './MenuContext';
  12. import SiderMenu from '@/components/SiderMenu';
  13. import getPageTitle from '@/utils/getPageTitle';
  14. import styles from './BasicLayout.less';
  15. // lazy load SettingDrawer
  16. const SettingDrawer = React.lazy(() => import('@/components/SettingDrawer'));
  17. const { Content } = Layout;
  18. const query = {
  19. 'screen-xs': {
  20. maxWidth: 575,
  21. },
  22. 'screen-sm': {
  23. minWidth: 576,
  24. maxWidth: 767,
  25. },
  26. 'screen-md': {
  27. minWidth: 768,
  28. maxWidth: 991,
  29. },
  30. 'screen-lg': {
  31. minWidth: 992,
  32. maxWidth: 1199,
  33. },
  34. 'screen-xl': {
  35. minWidth: 1200,
  36. maxWidth: 1599,
  37. },
  38. 'screen-xxl': {
  39. minWidth: 1600,
  40. },
  41. };
  42. class BasicLayout extends React.Component {
  43. componentDidMount() {
  44. const {
  45. dispatch,
  46. route: { routes, path, authority },
  47. } = this.props;
  48. dispatch({
  49. type: 'user/fetchCurrent',
  50. });
  51. dispatch({
  52. type: 'setting/getSetting',
  53. });
  54. dispatch({
  55. type: 'menu/getMenuData',
  56. payload: { routes, path, authority },
  57. });
  58. }
  59. getContext() {
  60. const { location, breadcrumbNameMap } = this.props;
  61. return {
  62. location,
  63. breadcrumbNameMap,
  64. };
  65. }
  66. getLayoutStyle = () => {
  67. const { fixSiderbar, isMobile, collapsed, layout } = this.props;
  68. if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
  69. return {
  70. paddingLeft: collapsed ? '80px' : '256px',
  71. };
  72. }
  73. return null;
  74. };
  75. handleMenuCollapse = collapsed => {
  76. const { dispatch } = this.props;
  77. dispatch({
  78. type: 'global/changeLayoutCollapsed',
  79. payload: collapsed,
  80. });
  81. };
  82. renderSettingDrawer = () => {
  83. // Do not render SettingDrawer in production
  84. // unless it is deployed in preview.pro.ant.design as demo
  85. if (process.env.NODE_ENV === 'production' && APP_TYPE !== 'site') {
  86. return null;
  87. }
  88. return <SettingDrawer />;
  89. };
  90. render() {
  91. const {
  92. navTheme,
  93. layout: PropsLayout,
  94. children,
  95. location: { pathname },
  96. isMobile,
  97. menuData,
  98. breadcrumbNameMap,
  99. fixedHeader,
  100. } = this.props;
  101. const isTop = PropsLayout === 'topmenu';
  102. const contentStyle = !fixedHeader ? { paddingTop: 0 } : {};
  103. const layout = (
  104. <Layout>
  105. {isTop && !isMobile ? null : (
  106. //侧边栏
  107. <SiderMenu
  108. logo={logo}
  109. theme={navTheme}
  110. onCollapse={this.handleMenuCollapse}
  111. menuData={menuData}
  112. isMobile={isMobile}
  113. {...this.props}
  114. />
  115. )}
  116. <Layout
  117. style={{
  118. ...this.getLayoutStyle(),
  119. minHeight: '100vh',
  120. }}
  121. >
  122. //头部
  123. <Header
  124. menuData={menuData}
  125. handleMenuCollapse={this.handleMenuCollapse}
  126. logo={logo}
  127. isMobile={isMobile}
  128. {...this.props}
  129. />
  130. //内容
  131. <Content className={styles.content} style={contentStyle}>
  132. {children}
  133. </Content>
  134. //底部
  135. <Footer />
  136. </Layout>
  137. </Layout>
  138. );
  139. return (
  140. <React.Fragment>
  141. <DocumentTitle title={getPageTitle(pathname, breadcrumbNameMap)}>
  142. <ContainerQuery query={query}>
  143. {params => (
  144. <Context.Provider value={this.getContext()}>
  145. <div className={classNames(params)}>{layout}</div>
  146. </Context.Provider>
  147. )}
  148. </ContainerQuery>
  149. </DocumentTitle>
  150. <Suspense fallback={null}>{this.renderSettingDrawer()}</Suspense>
  151. </React.Fragment>
  152. );
  153. }
  154. }
  155. export default connect(({ global, setting, menu: menuModel }) => ({
  156. collapsed: global.collapsed,
  157. layout: setting.layout,
  158. menuData: menuModel.menuData,
  159. breadcrumbNameMap: menuModel.breadcrumbNameMap,
  160. ...setting,
  161. }))(props => (
  162. <Media query="(max-width: 599px)">
  163. {isMobile => <BasicLayout {...props} isMobile={isMobile} />}
  164. </Media>
  165. ));

Env 搭建

前序准备

你的本地环境需要安装 yarn、node 和 git。我们的技术栈基于 ES2015+、React、UmiJS、dva、g2 和 antd,提前了解和学习这些知识会非常有帮助。

安装#

新建一个空的文件夹作为项目目录,并在目录下执行:

  1. yarn create umi

or

  1. npm create umi

选择 ant-design-pro:

  1. Select the boilerplate type (Use arrow keys)
  2. ❯ ant-design-pro - Create project with an layout-only ant-design-pro boilerplate, use together with umi block.
  3. app - Create project with a simple boilerplate, support typescript.
  4. block - Create a umi block.
  5. library - Create a library with umi.
  6. plugin - Create a umi plugin.

Ant Design Pro 脚手架将会自动安装。

目录结构#

我们已经为你生成了一个完整的开发框架,提供了涵盖中后台开发的各类功能和坑位,下面是整个项目的目录结构。

  1. ├── config # umi 配置,包含路由,构建等配置
  2. ├── mock # 本地模拟数据
  3. ├── public
  4. │ └── favicon.png # Favicon
  5. ├── src
  6. │ ├── assets # 本地静态资源
  7. │ ├── components # 业务通用组件
  8. │ ├── e2e # 集成测试用例
  9. │ ├── layouts # 通用布局
  10. │ ├── models # 全局 dva model
  11. │ ├── pages # 业务页面入口和常用模板
  12. │ ├── services # 后台接口服务
  13. │ ├── utils # 工具库
  14. │ ├── locales # 国际化资源
  15. │ ├── global.less # 全局样式
  16. │ └── global.ts # 全局 JS
  17. ├── tests # 测试工具
  18. ├── README.md
  19. └── package.json

本地开发#

安装依赖。

  1. npm install

如果网络状况不佳,可以使用 tyarn 进行加速。

  1. npm start

1:结构(Env) - 图1

启动完成后会自动打开浏览器访问 http://localhost:8000,你看到下面的页面就代表成功了。
1:结构(Env) - 图2

后续步骤

> 传统开发模式,完全自定义开发