使用dva+umi+antd构建页面(一)

使用dva+umi+antd构建页面

参考地址:https://ant.design/docs/react/practical-projects-cn

首先确保安装npm或者yarn

创建新应用

首先创建应用目录

mkdir myapp && cd myapp

推荐使用 yarn 创建应用,执行以下命令,

如果你坚持用 npm,可执行 npm install -g create-umi && create-umi,效果一致。

$ 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.9.5" with binaries:
      - create-umi

yarn 会先安装最新版的 create-umi,然后提供交互式的提示来创建应用。

选择 app, 然后回车确认。

? Select the boilerplate type 
  ant-design-pro  - Create project with an layout-only ant-design-pro boilerplate, use together with umi block. 
❯ app             - Create project with a simple boilerplate, support typescript. 
  block           - Create a umi block. 
  library         - Create a library with umi. 
  plugin          - Create a umi plugin. 

选上 antddva,然后回车确认。

   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.

然后安装依赖,

$ yarn

然后启动应用,

$ yarn start

几秒钟后,你会看到以下输出,

 DONE  Compiled successfully in 212ms

  App running at:
  - Local:   http://localhost:8000/
  - Network: http://{{ YourIP }}:8000/

在浏览器里打开 http://localhost:8000,你会看到 umi 的欢迎界面。

img

使用 antd#

前面选择 antd 之后,会自动处理 antd 的依赖以及按需加载。你可以检查 .umirc.js 里的配置,确保 antd 已开启。

// ref: https://umijs.org/config/
export default {
  plugins: [
    // ref: https://umijs.org/plugin/umi-plugin-react.html
    ['umi-plugin-react', {
      antd: true,
      dva: true,
    }],
  ],
}

而如果要使用固定版本的 antd,你可以在项目里安装额外的 antd 依赖,package.json 里声明的 antd 依赖会被优先使用。

新建路由#

我们要写个应用来先显示产品列表。首先第一步是创建路由,路由可以想象成是组成应用的不同页面。

如果你没有 npx,需要先安装他,用于执行 node_modules 下的命令,

$ yarn global add npx

然后通过命令创建 /products 路由,

$ npx umi g page products

   create src/pages/products.js
   create src/pages/products.css
✔  success

然后在浏览器里打开 http://localhost:8000/products,你应该能看到对应的页面。

编写 UI Component#

随着应用的发展,你会需要在多个页面分享 UI 元素 (或在一个页面使用多次),在 umi 里你可以把这部分抽成 component 。

我们来编写一个 ProductList component,这样就能在不同的地方显示产品列表了。

新建 src/components/ProductList.js 文件:

import { Table, Popconfirm, Button } from 'antd';

const ProductList = ({ onDelete, products }) => {
  const columns = [{
    title: 'Name',
    dataIndex: 'name',
  }, {
    title: 'Actions',
    render: (text, record) => {
      return (
        <Popconfirm title="Delete?" onConfirm={() => onDelete(record.id)}>
    <Button>Delete</Button>
      </Popconfirm>
    );
    },
  }];
  return (
    <Table
      dataSource={products}
      columns={columns}
    />
  );
};

export default ProductList;

定义 dva Model#

完成 UI 后,现在开始处理数据和逻辑。

dva 通过 model 的概念把一个领域的模型管理起来,包含同步更新 state 的 reducers,处理异步逻辑的 effects,订阅数据源的 subscriptions 。

新建 model src/models/products.js

export default {
  namespace: 'products',
  state: [],
  reducers: {
    'delete'(state, { payload: id }) {
      return state.filter(item => item.id !== id);
    },
  },
};

这个 model 里:

  • namespace 表示在全局 state 上的 key
  • state 是初始值,在这里是空数组
  • reducers 等同于 redux 里的 reducer,接收 action,同步更新 state

umi 里约定 src/models 下的 model 会被自动注入,你无需手动注入。

connect 起来#

到这里,我们已经单独完成了 model 和 component,那么他们如何串联起来呢?

dva 提供了 connect 方法。如果你熟悉 redux,这个 connect 来自 react-redux。

编辑 src/pages/products.js,替换为以下内容:

import { connect } from 'dva';
import ProductList from '../components/ProductList';

const Products = ({ dispatch, products }) => {
  function handleDelete(id) {
    dispatch({
      type: 'products/delete',
      payload: id,
    });
  }
  return (
    <div>
      <h2>List of Products</h2>
      <ProductList onDelete={handleDelete} products={products} />
    </div>
  );
};

export default connect(({ products }) => ({
  products,
}))(Products);

最后,我们还需要一些初始数据让这个应用 run 起来。编辑 src/app.js

export const dva = {
  config: {
    onError(err) {
      err.preventDefault();
      console.error(err.message);
    },
    initialState: {
      products: [
        { name: 'dva', id: 1 },
        { name: 'antd', id: 2 },
      ],
    },
  },
};

刷新浏览器,应该能看到以下效果:

img

构建应用#

完成开发并且在开发环境验证之后,就需要部署给我们的用户了。先执行下面的命令,

$ npm run build

几秒后,输出应该如下:

> @ build /private/tmp/sorrycc-V0lLrF
> umi build

[5:01:58 PM] webpack compiled in 11s 615ms


 DONE  Compiled successfully in 11622ms                                           5:01:58 PM

File sizes after gzip:

  340.44 KB  dist/umi.js
  17.82 KB   dist/umi.css

build 命令会打包所有的资源,包含 JavaScript, CSS, web fonts, images, html 等。你可以在 dist/ 目录下找到这些文件。

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值