mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-05 23:46:28 +08:00
doc: update getting-started and practical-projects with umi
This commit is contained in:
parent
236f48e8ce
commit
f9c0b07db2
@ -21,7 +21,7 @@ During development, you may need to compile and debug JSX and ES2015 code, and e
|
||||
|
||||
### 1. Installation
|
||||
|
||||
[antd-init](https://github.com/ant-design/antd-init/) is a demo-only scaffold tool. If you want to create real world projects, [dva-cli](https://github.com/dvajs/dva-cli) and [create-react-app](https://github.com/facebookincubator/create-react-app) is our recommendation.
|
||||
[antd-init](https://github.com/ant-design/antd-init/) is a demo-only scaffold tool. If you want to create real world projects, [create-umi](https://github.com/umijs/create-umi) and [create-react-app](https://github.com/facebookincubator/create-react-app) is our recommendation.
|
||||
|
||||
```bash
|
||||
$ npm install antd-init -g
|
||||
|
@ -23,7 +23,7 @@ Ant Design React 致力于提供给程序员**愉悦**的开发体验。
|
||||
|
||||
### 1. 安装脚手架工具
|
||||
|
||||
[antd-init](https://github.com/ant-design/antd-init/) 是一个用于演示 antd 如何使用的脚手架工具,实际业务项目建议使用 [dva-cli](https://github.com/dvajs/dva-cli) 和 [create-react-app](https://github.com/facebookincubator/create-react-app) 进行搭建。
|
||||
[antd-init](https://github.com/ant-design/antd-init/) 是一个用于演示 antd 如何使用的脚手架工具,实际业务项目建议使用 [create-umi](https://github.com/umijs/create-umi) 或 [create-react-app](https://github.com/facebookincubator/create-react-app) 进行搭建。
|
||||
|
||||
```bash
|
||||
$ npm install antd-init -g
|
||||
|
@ -1,115 +1,155 @@
|
||||
---
|
||||
order: 3
|
||||
title: Real project with dva
|
||||
title: Real project with umi and dva
|
||||
---
|
||||
|
||||
[dva](https://github.com/dvajs/dva) is a React and redux based, lightweight and elm-style framework, which supports side effects, hot module replacement, dynamic on demand, react-native, and SSR. It has been widely used in production environments.
|
||||
In real project development, you might need a data flow solution like Redux or MobX. Ant Design React is a UI library that can be used with any data flow solution and application framework within the React ecosystem. We have launched dva based on Redux, as well as a pluggable enterprise application framework umi, which is recommended for use in your projects.
|
||||
|
||||
This article will guide you to create a simple application from zero using dva and antd.
|
||||
Dva is a lightweight data flow solution based on Redux. The concept comes from elm. It supports side effects, hot module replacement, dynamic loading, react-native, SSR, etc. It has been widely used in production.
|
||||
|
||||
Include the following:
|
||||
And [umi](http://umijs.org/) is a routing-based framework that supports [next.js-like conventional routing](https://umijs.org/guide/router.html) and various advanced routing functions, such as [routing-level on-demand loading](https://umijs.org/en/plugin/umi-plugin-react.html#dynamicimport). With a complete [plugin system](https://umijs.org/plugin/) that covers every life cycle from source code to build product, umi is able to support various functional extensions and business needs.
|
||||
|
||||
---
|
||||
> You may also be interested in [Ant Design Pro] (https://pro.ant.design/), an Out-of-box UI solution for enterprise applications based on umi, dva and ant design.
|
||||
|
||||
## Install dva-cli
|
||||
|
||||
Install dva-cli with npm, and make sure the version is later than `0.9.1`.
|
||||
|
||||
```bash
|
||||
$ npm install dva-cli -g
|
||||
$ dva -v
|
||||
dva-cli version 0.9.1
|
||||
```
|
||||
This article will guide you to create a simple application from zero using umi, dva and antd.
|
||||
|
||||
## Create New App
|
||||
|
||||
After you have installed dva-cli, you can have access to the `dva` command in terminal ([can't access?](http://stackoverflow.com/questions/15054388/global-node-modules-not-installing-correctly-command-not-found)). Now, create a new application with `dva new`.
|
||||
First create an empty directory,
|
||||
|
||||
```bash
|
||||
$ dva new dva-quickstart
|
||||
$ mkdir myapp
|
||||
$ cd myapp
|
||||
```
|
||||
|
||||
This creates a `dva-quickstart` directory, that contains the project directories and files, and provides a development server, build script, mock service, proxy server and so on.
|
||||
It is recommended to use yarn to create an application and execute the following command.
|
||||
|
||||
Then `cd` to the `dva-quickstart` directory, and start the development server.
|
||||
> If you insist on using npm, execute `npm install -g create-umi && create-umi` and the effect will be the same.
|
||||
|
||||
```bash
|
||||
$ cd dva-quickstart
|
||||
$ npm start
|
||||
$ yarn create umi
|
||||
|
||||
yarn create v1.12.0
|
||||
[1/4] 🔍 Resolving packages...
|
||||
[2/4] 🚚 Fetching packages...
|
||||
[3/4] 🔗 Linking dependencies...
|
||||
[4/4] 📃 Building fresh packages...
|
||||
|
||||
success Installed "create-umi@0.3.1" with binaries:
|
||||
- create-umi
|
||||
|
||||
? What functionality do your want to enable? (Press <space> to select, <a> to toggle all, <i
|
||||
> to invert selection)
|
||||
❯◯ antd
|
||||
◯ dva
|
||||
◯ code splitting
|
||||
◯ pwa
|
||||
◯ dll
|
||||
◯ hard source
|
||||
```
|
||||
|
||||
After a few seconds, you will see the following output:
|
||||
Yarn will install the latest version of [create-umi](https://github.com/umijs/create-umi) and then create the app with interactive ui.
|
||||
|
||||
Select `antd` and `dva` and press Enter to confirm.
|
||||
|
||||
```bash
|
||||
Compiled successfully!
|
||||
|
||||
The app is running at:
|
||||
|
||||
http://localhost:8000/
|
||||
|
||||
Note that the development build is not optimized.
|
||||
To create a production build, use npm run build.
|
||||
create package.json
|
||||
create mock/.gitkeep
|
||||
create src/assets/yay.jpg
|
||||
create src/layouts/index.css
|
||||
create src/layouts/index.js
|
||||
create src/pages/index.css
|
||||
create src/pages/index.js
|
||||
create src/global.css
|
||||
create .gitignore
|
||||
create .editorconfig
|
||||
create .env
|
||||
create .umirc.js
|
||||
create .eslintrc
|
||||
create .prettierrc
|
||||
create .prettierignore
|
||||
create src/models/.gitkeep
|
||||
create src/dva.js
|
||||
✨ File Generate Done
|
||||
✨ Done in 966.73s.
|
||||
```
|
||||
|
||||
Open http://localhost:8000 in your browser, you will see the dva welcome page.
|
||||
Then install dependencies,
|
||||
|
||||
```bash
|
||||
$ yarn
|
||||
```
|
||||
|
||||
Then start the app,
|
||||
|
||||
```bash
|
||||
$ yarn start
|
||||
```
|
||||
|
||||
After a few seconds, you will see the following output,
|
||||
|
||||
```bash
|
||||
DONE Compiled successfully in 212ms
|
||||
|
||||
App running at:
|
||||
- Local: http://localhost:8000/
|
||||
- Network: http://{{ YourIP }}:8000/
|
||||
```
|
||||
|
||||
Open [http://localhost:8000](http://localhost:8000) in your browser, you will see the welcome page of umi.
|
||||
|
||||
<img src="https://gw.alipayobjects.com/zos/rmsportal/lewbQdlEHzuNDpaxykUP.png" width="718" />
|
||||
|
||||
## Integrate antd
|
||||
|
||||
Install `antd` and `babel-plugin-import` with npm. `babel-plugin-import` is used to automatically import scripts and stylesheets from antd on demand. See [repo](https://github.com/ant-design/babel-plugin-import) 。
|
||||
After selecting `antd` earlier, antd's dependencies are automatically handled and loaded on demand. You can check the configuration in `.umirc.js` to make sure antd is turned on.
|
||||
|
||||
```bash
|
||||
$ npm install antd babel-plugin-import --save
|
||||
```
|
||||
|
||||
Edit `.webpackrc` to integrate `babel-plugin-import`.
|
||||
|
||||
```diff
|
||||
{
|
||||
+ "extraBabelPlugins": [
|
||||
+ ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": "css" }]
|
||||
+ ]
|
||||
```js
|
||||
// ref: https://umijs.org/config/
|
||||
export default {
|
||||
plugins: [
|
||||
// ref: https://umijs.org/plugin/umi-plugin-react.html
|
||||
['umi-plugin-react', {
|
||||
antd: true,
|
||||
dva: true,
|
||||
}],
|
||||
],
|
||||
}
|
||||
```
|
||||
|
||||
> Notice: dva-cli's build and dev is based on roadhog, view [roadhog#Configuration](https://github.com/sorrycc/roadhog/blob/master/README_en-us.md#configuration) for more `.webpackrc` Configuration.
|
||||
> And if you want to use a fixed version of antd, you can install additional antd dependency in your project, and the antd dependencies declared in package.json will be used first.
|
||||
|
||||
## Define Router
|
||||
## Create Routes
|
||||
|
||||
We need to write an application displaying the list of products. The first step is to create a route.
|
||||
|
||||
Create a route component `routes/Products.js`:
|
||||
If you don't have npx, you need to install it first to execute the commands under node_modules.
|
||||
|
||||
```javascript
|
||||
import React from 'react';
|
||||
|
||||
const Products = (props) => (
|
||||
<h2>List of Products</h2>
|
||||
);
|
||||
|
||||
export default Products;
|
||||
```bash
|
||||
$ yarn global addd npx
|
||||
```
|
||||
|
||||
Add routing information to router, edit `router.js`:
|
||||
Then create a `/products` route,
|
||||
|
||||
```diff
|
||||
+ import Products from './routes/Products';
|
||||
...
|
||||
+ <Route path="/products" exact component={Products} />
|
||||
```bash
|
||||
$ npx umi g page products
|
||||
|
||||
create src/pages/products.js
|
||||
create src/pages/products.css
|
||||
✔ success
|
||||
```
|
||||
|
||||
Then open http://localhost:8000/#/products in your browser, you should be able to see the `<h2>` tag defined before.
|
||||
Then open [http://localhost:8000/products](http://localhost:8000/products) in your browser and you should see the corresponding page.
|
||||
|
||||
## Write UI Components
|
||||
|
||||
As your application grows and you notice you are sharing UI elements between multiple pages (or using them multiple times on the same page), in dva it's called reusable components.
|
||||
As your application grows and you notice you are sharing UI elements between multiple pages (or using them multiple times on the same page), in umi it's called reusable components.
|
||||
|
||||
Let's create a `ProductList` component that we can use in multiple places to show a list of products.
|
||||
|
||||
Create `components/ProductList.js` by typing:
|
||||
Create `src/components/ProductList.js` by typing:
|
||||
|
||||
```javascript
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
```js
|
||||
import { Table, Popconfirm, Button } from 'antd';
|
||||
|
||||
const ProductList = ({ onDelete, products }) => {
|
||||
@ -121,9 +161,9 @@ const ProductList = ({ onDelete, products }) => {
|
||||
render: (text, record) => {
|
||||
return (
|
||||
<Popconfirm title="Delete?" onConfirm={() => onDelete(record.id)}>
|
||||
<Button>Delete</Button>
|
||||
</Popconfirm>
|
||||
);
|
||||
<Button>Delete</Button>
|
||||
</Popconfirm>
|
||||
);
|
||||
},
|
||||
}];
|
||||
return (
|
||||
@ -134,23 +174,18 @@ const ProductList = ({ onDelete, products }) => {
|
||||
);
|
||||
};
|
||||
|
||||
ProductList.proptypes = {
|
||||
onDelete: PropTypes.func.isRequired,
|
||||
products: PropTypes.array.isRequired,
|
||||
};
|
||||
|
||||
export default ProductList;
|
||||
```
|
||||
|
||||
## Define Model
|
||||
## Define dva Model
|
||||
|
||||
After completing the UI, we will begin processing the data and logic.
|
||||
|
||||
dva manages the domain model with `model`, with reducers for synchronous state updates, effects for async logic, and subscriptions for data source subscribe.
|
||||
|
||||
Let's create a model `models/products.js` by typing:
|
||||
Let's create a model `src/models/products.js` by typing,
|
||||
|
||||
```javascript
|
||||
```js
|
||||
export default {
|
||||
namespace: 'products',
|
||||
state: [],
|
||||
@ -164,27 +199,21 @@ export default {
|
||||
|
||||
In this model:
|
||||
|
||||
- `namespace` represents the key on global state
|
||||
- `state` is the initial value, here it is an empty array
|
||||
- `reducers` is equivalent to a reducer in redux, accepting an action, and update state simultaneously
|
||||
* `namespace` represents the key on global state
|
||||
* `state` is the initial value, here it is an empty array
|
||||
* `reducers` is equivalent to a reducer in redux, accepting an action, and update state simultaneously
|
||||
|
||||
Then don't forget to require it in `index.js`:
|
||||
|
||||
```diff
|
||||
// 3. Model
|
||||
+ app.model(require('./models/products').default);
|
||||
```
|
||||
In umi, the model files under `src/models` will be automatically injected, you don't need to inject manually.
|
||||
|
||||
## Connect
|
||||
|
||||
So far, we have completed a separate model and component. How do we connect them together?
|
||||
|
||||
dva provides a `connect` method. If you are familiar with redux, this `connect` is from react-router.
|
||||
dva provides a `connect` method. If you are familiar with redux, this connect is from react-redux.
|
||||
|
||||
Edit `routes/Products.js` and replace it with the following:
|
||||
Edit `src/pages/products.js` and replact it with the following,
|
||||
|
||||
```javascript
|
||||
import React from 'react';
|
||||
```js
|
||||
import { connect } from 'dva';
|
||||
import ProductList from '../components/ProductList';
|
||||
|
||||
@ -203,74 +232,75 @@ const Products = ({ dispatch, products }) => {
|
||||
);
|
||||
};
|
||||
|
||||
// export default Products;
|
||||
export default connect(({ products }) => ({
|
||||
products,
|
||||
}))(Products);
|
||||
```
|
||||
|
||||
Finally, we need some initial data to make the application run together. Edit `index.js`:
|
||||
Finally, we need some initial data to make the application run together. Edit `src/app.js`,
|
||||
|
||||
```diff
|
||||
- const app = dva();
|
||||
+ const app = dva({
|
||||
+ initialState: {
|
||||
+ products: [
|
||||
+ { name: 'dva', id: 1 },
|
||||
+ { name: 'antd', id: 2 },
|
||||
+ ],
|
||||
+ },
|
||||
+ });
|
||||
```js
|
||||
export const dva = {
|
||||
config: {
|
||||
onError(err) {
|
||||
err.preventDefault();
|
||||
console.error(err.message);
|
||||
},
|
||||
initialState: {
|
||||
products: [
|
||||
{ name: 'dva', id: 1 },
|
||||
{ name: 'antd', id: 2 },
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
Refresh your browser, you should see the following result:
|
||||
|
||||
<p style="text-align: center">
|
||||
<img src="https://zos.alipayobjects.com/rmsportal/GQJeDDeUCSTRMMg.gif" />
|
||||
</p>
|
||||
<img src="https://zos.alipayobjects.com/rmsportal/GQJeDDeUCSTRMMg.gif" />
|
||||
|
||||
## Build
|
||||
|
||||
Now that we've written our application and verified that it works in development, it's time to get it ready for deployment to our users. To do so, run the following command:
|
||||
Now that we've written our application and verified that it works in development, it's time to get it ready for deployment to our users. To do so, run the following command,
|
||||
|
||||
```bash
|
||||
$ npm run build
|
||||
```
|
||||
|
||||
After a few seconds, the output should be as follows:
|
||||
After a few seconds, the output should be as follows,
|
||||
|
||||
```bash
|
||||
> @ build /private/tmp/myapp
|
||||
> roadhog build
|
||||
> @ build /private/tmp/sorrycc-V0lLrF
|
||||
> umi build
|
||||
|
||||
Creating an optimized production build...
|
||||
Compiled successfully.
|
||||
[5:01:58 PM] webpack compiled in 11s 615ms
|
||||
|
||||
|
||||
DONE Compiled successfully in 11622ms 5:01:58 PM
|
||||
|
||||
File sizes after gzip:
|
||||
|
||||
82.98 KB dist/index.js
|
||||
270 B dist/index.css
|
||||
340.44 KB dist/umi.js
|
||||
17.82 KB dist/umi.css
|
||||
```
|
||||
|
||||
The `build` command packages up all of the assets that make up your application —— JavaScript, templates, CSS, web fonts, images, and more. Then you can find these files in the `dist /` directory.
|
||||
The `build` command packages up all of the assets that make up your application —— JavaScript, templates, CSS, web fonts, images, and more. Then you can find these files in the `dist/` directory.
|
||||
|
||||
## What's Next
|
||||
|
||||
We have completed a simple application, but you may still have lots of questions, such as:
|
||||
|
||||
- How to deal with async logic
|
||||
- How to load initial data elegantly
|
||||
- How to handle onError globally and locally
|
||||
- How to load Routes and Models on demand
|
||||
- How to implement HMR
|
||||
- How to mock data
|
||||
- and so on...
|
||||
* How to handle onError globally and locally?
|
||||
* How to handle routes?
|
||||
* How to mock data?
|
||||
* How to deploy?
|
||||
* ant so on...
|
||||
|
||||
You can:
|
||||
|
||||
- Visit [dva official website](https://dvajs.com/).
|
||||
- Be familiar with the [8 Concepts](https://dvajs.com/guide/concepts.html), and understand how they are connected together
|
||||
- Know all [dva APIs](https://dvajs.com/api/)
|
||||
- Checkout [dva knowledgemap](https://dvajs.com/knowledgemap/), including all the basic knowledge with ES6, React, dva
|
||||
- Checkout [more FAQ](https://github.com/dvajs/dva/issues?q=is%3Aissue+is%3Aclosed+label%3Afaq)
|
||||
- If your project is created with [dva-cli](https://github.com/dvajs/dva-cli) , checkout how to [Configure it](https://github.com/sorrycc/roadhog#configuration)
|
||||
* Visit [umi offcial website](https://umijs.org/) and [dva offcial website](https://dvajs.com/)
|
||||
* Know [the umi routes](https://umijs.org/zh/guide/router.html)
|
||||
* Know [how to deploy umi application](https://umijs.org/zh/guide/deploy.html)
|
||||
* Checkout [dva knowledge](https://dvajs.com/knowledgemap/), including all the basic knowledge with ES6, React, dva
|
||||
* Be familiar with the [8 Concepts of dva](https://dvajs.com/guide/concepts.html), and understand how they are connected together
|
||||
|
@ -3,115 +3,153 @@ order: 3
|
||||
title: 项目实战
|
||||
---
|
||||
|
||||
在真实项目开发中,你可能会需要 Redux 或者 MobX 这样的数据应用框架,Ant Design React 作为一个 UI 库,可以和任何 React 生态圈内的应用框架搭配使用。我们也基于 Redux 推出了自己的最佳实践 dva,推荐你在项目中使用。
|
||||
在真实项目开发中,你可能会需要 Redux 或者 MobX 这样的数据流方案,Ant Design React 作为一个 UI 库,可以和任何 React 生态圈内的数据流方案以及应用框架搭配使用。我们基于 Redux 推出了自己的最佳实践 dva,以及可插拔的企业级应用框架 umi,推荐你在项目中使用。
|
||||
|
||||
[dva](https://github.com/dvajs/dva) 是一个基于 React 和 Redux 的轻量应用框架,概念来自 elm,支持 side effects、热替换、动态加载、react-native、SSR 等,已在生产环境广泛应用。
|
||||
[dva](http://dvajs.com/) 是一个基于 Redux 的 轻量级数据流方案,概念来自 elm,支持 side effects、热替换、动态加载、react-native、SSR 等,已在生产环境广泛应用。
|
||||
|
||||
本文会引导你使用 dva 和 antd 从 0 开始创建一个简单应用。
|
||||
[umi](http://umijs.org/) 则是一个可插拔的企业级 react 应用框架。umi 以路由为基础的,支持[类 next.js 的约定式路由](https://umijs.org/zh/guide/router.html),以及各种进阶的路由功能,并以此进行功能扩展,比如[支持路由级的按需加载](https://umijs.org/zh/plugin/umi-plugin-react.html#dynamicimport)。然后配以完善的[插件体系](https://umijs.org/zh/plugin/),覆盖从源码到构建产物的每个生命周期,支持各种功能扩展和业务需求。
|
||||
|
||||
会包含以下内容:
|
||||
> 你可能也会对 [Ant Design Pro](https://pro.ant.design/) 感兴趣,这是一个基于 umi、dva 和 ant design 的开箱即用的中台前端/设计解决方案。
|
||||
|
||||
---
|
||||
|
||||
## 安装 dva-cli
|
||||
|
||||
通过 npm 安装 dva-cli 并确保版本是 `0.9.1` 或以上。
|
||||
|
||||
```bash
|
||||
$ npm install dva-cli -g
|
||||
$ dva -v
|
||||
dva-cli version 0.9.1
|
||||
```
|
||||
本文会引导你使用 umi、dva 和 antd 从 0 开始创建一个简单应用。
|
||||
|
||||
## 创建新应用
|
||||
|
||||
安装完 dva-cli 之后,就可以在命令行里访问到 `dva` 命令([不能访问?](http://stackoverflow.com/questions/15054388/global-node-modules-not-installing-correctly-command-not-found))。现在,你可以通过 `dva new` 创建新应用。
|
||||
先创建一个空目录,
|
||||
|
||||
```bash
|
||||
$ dva new dva-quickstart
|
||||
$ mkdir myapp
|
||||
$ cd myapp
|
||||
```
|
||||
|
||||
这会创建 `dva-quickstart` 目录,包含项目初始化目录和文件,并提供开发服务器、构建脚本、数据 mock 服务、代理服务器等功能。
|
||||
推荐使用 yarn 创建应用,执行以下命令,
|
||||
|
||||
然后我们 `cd` 进入 `dva-quickstart` 目录,并启动开发服务器:
|
||||
> 如果你坚持用 npm,可执行 `npm install -g create-umi && create-umi`,效果一致。
|
||||
|
||||
```bash
|
||||
$ cd dva-quickstart
|
||||
$ npm start
|
||||
$ yarn create umi
|
||||
|
||||
yarn create v1.12.0
|
||||
[1/4] 🔍 Resolving packages...
|
||||
[2/4] 🚚 Fetching packages...
|
||||
[3/4] 🔗 Linking dependencies...
|
||||
[4/4] 📃 Building fresh packages...
|
||||
|
||||
success Installed "create-umi@0.3.1" with binaries:
|
||||
- create-umi
|
||||
|
||||
? What functionality do your want to enable? (Press <space> to select, <a> to toggle all, <i
|
||||
> to invert selection)
|
||||
❯◯ antd
|
||||
◯ dva
|
||||
◯ code splitting
|
||||
◯ pwa
|
||||
◯ dll
|
||||
◯ hard source
|
||||
```
|
||||
|
||||
几秒钟后,你会看到以下输出:
|
||||
yarn 会先安装最新版的 [create-umi](https://github.com/umijs/create-umi),然后提供交互式的提示来创建应用。
|
||||
|
||||
选上 `antd` 和 `dva`,然后回车确认。
|
||||
|
||||
```
|
||||
create package.json
|
||||
create mock/.gitkeep
|
||||
create src/assets/yay.jpg
|
||||
create src/layouts/index.css
|
||||
create src/layouts/index.js
|
||||
create src/pages/index.css
|
||||
create src/pages/index.js
|
||||
create src/global.css
|
||||
create .gitignore
|
||||
create .editorconfig
|
||||
create .env
|
||||
create .umirc.js
|
||||
create .eslintrc
|
||||
create .prettierrc
|
||||
create .prettierignore
|
||||
create src/models/.gitkeep
|
||||
create src/dva.js
|
||||
✨ File Generate Done
|
||||
✨ Done in 966.73s.
|
||||
```
|
||||
|
||||
然后安装依赖,
|
||||
|
||||
```bash
|
||||
Compiled successfully!
|
||||
|
||||
The app is running at:
|
||||
|
||||
http://localhost:8000/
|
||||
|
||||
Note that the development build is not optimized.
|
||||
To create a production build, use npm run build.
|
||||
$ yarn
|
||||
```
|
||||
|
||||
在浏览器里打开 http://localhost:8000 ,你会看到 dva 的欢迎界面。
|
||||
然后启动应用,
|
||||
|
||||
```bash
|
||||
$ yarn start
|
||||
```
|
||||
|
||||
几秒钟后,你会看到以下输出,
|
||||
|
||||
```bash
|
||||
DONE Compiled successfully in 212ms
|
||||
|
||||
App running at:
|
||||
- Local: http://localhost:8000/
|
||||
- Network: http://{{ YourIP }}:8000/
|
||||
```
|
||||
|
||||
在浏览器里打开 [http://localhost:8000](http://localhost:8000),你会看到 umi 的欢迎界面。
|
||||
|
||||
<img src="https://gw.alipayobjects.com/zos/rmsportal/lewbQdlEHzuNDpaxykUP.png" width="718" />
|
||||
|
||||
## 使用 antd
|
||||
|
||||
通过 npm 安装 `antd` 和 `babel-plugin-import` 。`babel-plugin-import` 是用来按需加载 antd 的脚本和样式的,详见 [repo](https://github.com/ant-design/babel-plugin-import) 。
|
||||
前面选择 antd 之后,会自动处理 antd 的依赖以及按需加载。你可以检查 `.umirc.js` 里的配置,确保 antd 已开启。
|
||||
|
||||
```bash
|
||||
$ npm install antd babel-plugin-import --save
|
||||
```
|
||||
|
||||
编辑 `.webpackrc`,使 `babel-plugin-import` 插件生效。
|
||||
|
||||
```diff
|
||||
{
|
||||
+ "extraBabelPlugins": [
|
||||
+ ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": "css" }]
|
||||
+ ]
|
||||
```js
|
||||
// ref: https://umijs.org/config/
|
||||
export default {
|
||||
plugins: [
|
||||
// ref: https://umijs.org/plugin/umi-plugin-react.html
|
||||
['umi-plugin-react', {
|
||||
antd: true,
|
||||
dva: true,
|
||||
}],
|
||||
],
|
||||
}
|
||||
```
|
||||
|
||||
> 注:dva-cli 基于 roadhog 实现 build 和 dev,更多 `.webpackrc` 的配置详见 [roadhog#配置](https://github.com/sorrycc/roadhog#配置)
|
||||
> 而如果要使用固定版本的 antd,你可以在项目里安装额外的 antd 依赖,package.json 里声明的 antd 依赖会被优先使用。
|
||||
|
||||
## 定义路由
|
||||
## 新建路由
|
||||
|
||||
我们要写个应用来先显示产品列表。首先第一步是创建路由,路由可以想象成是组成应用的不同页面。
|
||||
|
||||
新建 route component `routes/Products.js`,内容如下:
|
||||
如果你没有 npx,需要先安装他,用于执行 node_modules 下的命令,
|
||||
|
||||
```javascript
|
||||
import React from 'react';
|
||||
|
||||
const Products = (props) => (
|
||||
<h2>List of Products</h2>
|
||||
);
|
||||
|
||||
export default Products;
|
||||
```bash
|
||||
$ yarn global addd npx
|
||||
```
|
||||
|
||||
添加路由信息到路由表,编辑 `router.js` :
|
||||
然后通过命令创建 `/products` 路由,
|
||||
|
||||
```diff
|
||||
+ import Products from './routes/Products';
|
||||
...
|
||||
+ <Route path="/products" exact component={Products} />
|
||||
```bash
|
||||
$ npx umi g page products
|
||||
|
||||
create src/pages/products.js
|
||||
create src/pages/products.css
|
||||
✔ success
|
||||
```
|
||||
|
||||
然后在浏览器里打开 http://localhost:8000/#/products ,你应该能看到前面定义的 `<h2>` 标签。
|
||||
然后在浏览器里打开 [http://localhost:8000/products](http://localhost:8000/products),你应该能看到对应的页面。
|
||||
|
||||
## 编写 UI Component
|
||||
|
||||
随着应用的发展,你会需要在多个页面分享 UI 元素 (或在一个页面使用多次),在 dva 里你可以把这部分抽成 component 。
|
||||
随着应用的发展,你会需要在多个页面分享 UI 元素 (或在一个页面使用多次),在 umi 里你可以把这部分抽成 component 。
|
||||
|
||||
我们来编写一个 `ProductList` component,这样就能在不同的地方显示产品列表了。
|
||||
|
||||
新建 `components/ProductList.js` 文件:
|
||||
新建 `src/components/ProductList.js` 文件:
|
||||
|
||||
```javascript
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
```js
|
||||
import { Table, Popconfirm, Button } from 'antd';
|
||||
|
||||
const ProductList = ({ onDelete, products }) => {
|
||||
@ -123,9 +161,9 @@ const ProductList = ({ onDelete, products }) => {
|
||||
render: (text, record) => {
|
||||
return (
|
||||
<Popconfirm title="Delete?" onConfirm={() => onDelete(record.id)}>
|
||||
<Button>Delete</Button>
|
||||
</Popconfirm>
|
||||
);
|
||||
<Button>Delete</Button>
|
||||
</Popconfirm>
|
||||
);
|
||||
},
|
||||
}];
|
||||
return (
|
||||
@ -136,23 +174,18 @@ const ProductList = ({ onDelete, products }) => {
|
||||
);
|
||||
};
|
||||
|
||||
ProductList.propTypes = {
|
||||
onDelete: PropTypes.func.isRequired,
|
||||
products: PropTypes.array.isRequired,
|
||||
};
|
||||
|
||||
export default ProductList;
|
||||
```
|
||||
|
||||
## 定义 Model
|
||||
## 定义 dva Model
|
||||
|
||||
完成 UI 后,现在开始处理数据和逻辑。
|
||||
|
||||
dva 通过 model 的概念把一个领域的模型管理起来,包含同步更新 state 的 reducers,处理异步逻辑的 effects,订阅数据源的 subscriptions 。
|
||||
dva 通过 `model` 的概念把一个领域的模型管理起来,包含同步更新 state 的 reducers,处理异步逻辑的 effects,订阅数据源的 subscriptions 。
|
||||
|
||||
新建 model `models/products.js` :
|
||||
新建 model `src/models/products.js`,
|
||||
|
||||
```javascript
|
||||
```js
|
||||
export default {
|
||||
namespace: 'products',
|
||||
state: [],
|
||||
@ -166,27 +199,21 @@ export default {
|
||||
|
||||
这个 model 里:
|
||||
|
||||
- `namespace` 表示在全局 state 上的 key
|
||||
- `state` 是初始值,在这里是空数组
|
||||
- `reducers` 等同于 redux 里的 reducer,接收 action,同步更新 state
|
||||
* `namespace` 表示在全局 state 上的 key
|
||||
* `state` 是初始值,在这里是空数组
|
||||
* `reducers` 等同于 redux 里的 reducer,接收 action,同步更新 state
|
||||
|
||||
然后别忘记在 `index.js` 里载入他:
|
||||
|
||||
```diff
|
||||
// 3. Model
|
||||
+ app.model(require('./models/products').default);
|
||||
```
|
||||
umi 里约定 `src/models` 下的 model 会被自动注入,你无需手动注入。
|
||||
|
||||
## connect 起来
|
||||
|
||||
到这里,我们已经单独完成了 model 和 component,那么他们如何串联起来呢?
|
||||
|
||||
dva 提供了 connect 方法。如果你熟悉 redux,这个 connect 就是 react-redux 的 connect 。
|
||||
dva 提供了 `connect` 方法。如果你熟悉 redux,这个 connect 来自 react-redux。
|
||||
|
||||
编辑 `routes/Products.js`,替换为以下内容:
|
||||
编辑 `src/pages/products.js`,替换为以下内容:
|
||||
|
||||
```javascript
|
||||
import React from 'react';
|
||||
```js
|
||||
import { connect } from 'dva';
|
||||
import ProductList from '../components/ProductList';
|
||||
|
||||
@ -205,35 +232,37 @@ const Products = ({ dispatch, products }) => {
|
||||
);
|
||||
};
|
||||
|
||||
// export default Products;
|
||||
export default connect(({ products }) => ({
|
||||
products,
|
||||
}))(Products);
|
||||
```
|
||||
|
||||
最后,我们还需要一些初始数据让这个应用 run 起来。编辑 `index.js`:
|
||||
最后,我们还需要一些初始数据让这个应用 run 起来。编辑 `src/app.js`:
|
||||
|
||||
```diff
|
||||
- const app = dva();
|
||||
+ const app = dva({
|
||||
+ initialState: {
|
||||
+ products: [
|
||||
+ { name: 'dva', id: 1 },
|
||||
+ { name: 'antd', id: 2 },
|
||||
+ ],
|
||||
+ },
|
||||
+ });
|
||||
```js
|
||||
export const dva = {
|
||||
config: {
|
||||
onError(err) {
|
||||
err.preventDefault();
|
||||
console.error(err.message);
|
||||
},
|
||||
initialState: {
|
||||
products: [
|
||||
{ name: 'dva', id: 1 },
|
||||
{ name: 'antd', id: 2 },
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
刷新浏览器,应该能看到以下效果:
|
||||
|
||||
<p style="text-align: center">
|
||||
<img src="https://zos.alipayobjects.com/rmsportal/GQJeDDeUCSTRMMg.gif" />
|
||||
</p>
|
||||
<img src="https://zos.alipayobjects.com/rmsportal/GQJeDDeUCSTRMMg.gif" />
|
||||
|
||||
## 构建应用
|
||||
|
||||
完成开发并且在开发环境验证之后,就需要部署给我们的用户了。先执行下面的命令:
|
||||
完成开发并且在开发环境验证之后,就需要部署给我们的用户了。先执行下面的命令,
|
||||
|
||||
```bash
|
||||
$ npm run build
|
||||
@ -242,37 +271,36 @@ $ npm run build
|
||||
几秒后,输出应该如下:
|
||||
|
||||
```bash
|
||||
> @ build /private/tmp/myapp
|
||||
> roadhog build
|
||||
> @ build /private/tmp/sorrycc-V0lLrF
|
||||
> umi build
|
||||
|
||||
Creating an optimized production build...
|
||||
Compiled successfully.
|
||||
[5:01:58 PM] webpack compiled in 11s 615ms
|
||||
|
||||
|
||||
DONE Compiled successfully in 11622ms 5:01:58 PM
|
||||
|
||||
File sizes after gzip:
|
||||
|
||||
82.98 KB dist/index.js
|
||||
270 B dist/index.css
|
||||
340.44 KB dist/umi.js
|
||||
17.82 KB dist/umi.css
|
||||
```
|
||||
|
||||
`build` 命令会打包所有的资源,包含 JavaScript, CSS, web fonts, images, html 等。然后你可以在 `dist/` 目录下找到这些文件。
|
||||
build 命令会打包所有的资源,包含 JavaScript, CSS, web fonts, images, html 等。你可以在 `dist/` 目录下找到这些文件。
|
||||
|
||||
## 下一步
|
||||
|
||||
我们已经完成了一个简单应用,你可能还有很多疑问,比如:
|
||||
|
||||
- 如何处理异步请求
|
||||
- 如何优雅地加载初始数据
|
||||
- 如何统一处理出错,以及特定操作的出错
|
||||
- 如何动态加载路由和 Model,以加速页面载入速度
|
||||
- 如何实现 hmr
|
||||
- 如何 mock 数据
|
||||
- 等等
|
||||
* 如何统一处理出错?
|
||||
* 如何处理更多路由,比如动态路由,嵌套路由,权限路由等?
|
||||
* 如何 mock 数据?
|
||||
* 如何部署?
|
||||
* 等等
|
||||
|
||||
你可以:
|
||||
|
||||
- 访问 [dva 官网](https://dvajs.com/)
|
||||
- 理解 dva 的 [8 个概念](https://dvajs.com/guide/concepts.html) ,以及他们是如何串起来的
|
||||
- 掌握 dva 的[所有 API](https://dvajs.com/api/)
|
||||
- 查看 [dva 知识地图](https://dvajs.com/knowledgemap/) ,包含 ES6, React, dva 等所有基础知识
|
||||
- 查看 [更多 FAQ](https://github.com/dvajs/dva/issues?q=is%3Aissue+is%3Aclosed+label%3Afaq),看看别人通常会遇到什么问题
|
||||
- 如果你基于 dva-cli 创建项目,最好了解他的 [配置方式](https://github.com/sorrycc/roadhog/blob/master/README_zh-cn.md#配置)
|
||||
* 访问 [umi 官网](https://umijs.org/)和 [dva 官网](https://dvajs.com/)
|
||||
* 理解 [umi 的路由](https://umijs.org/zh/guide/router.html)
|
||||
* 理解 [如何部署 umi 应用](https://umijs.org/zh/guide/deploy.html)
|
||||
* 查看 [dva 知识地图](https://dvajs.com/knowledgemap/),包含 ES6, React, dva 等所有基础知识
|
||||
* 理解 [dva 的 8 个概念](https://dvajs.com/guide/concepts.html),以及他们是如何串起来的
|
||||
|
Loading…
Reference in New Issue
Block a user