项目实战

    则是一个可插拔的企业级 react 应用框架。umi 以路由为基础的,支持类 next.js 的约定式路由,以及各种进阶的路由功能,并以此进行功能扩展,比如。然后配以完善的插件体系,覆盖从源码到构建产物的每个生命周期,支持各种功能扩展和业务需求,同时提供 通过可视化辅助编程(VAP)提高开发体验和研发效率。

    本文会引导你使用 Umi、dva 和 antd 从 0 开始创建一个简单应用。

    推荐使用 yarn 创建 Umi 脚手架,执行以下命令。

    如果你使用 npm,可执行 ,效果一致。

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

    新建路由

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

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

    1. $ npx umi g page products --typescript
    2. Write: src/pages/products.tsx
    3. Write: src/pages/products.css

    .umirc.ts 中配置路由,如果有国际化需要,可以配置 locale 开启 antd 国际化:

    1. import { defineConfig } from 'umi';
    2. export default defineConfig({
    3. + locale: { antd: true },
    4. routes: [
    5. { path: '/', component: '@/pages/index' },
    6. + { path: '/products', component: '@/pages/products' },
    7. ],
    8. });

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

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

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

    然后新建 src/components/ProductList.tsx 文件:

    简单数据流方案

    文件名则对应最终 model 的 name,你可以通过插件提供的 API 来消费 model 中的数据。

    我们以一个简单的表格作为示例。首先需要新建文件 src/models/useProductList.ts

    1. import { useRequest } from 'umi';
    2. import { queryProductList } from '@/services/product';
    3. export default function useProductList(params: { pageSize: number; current: number }) {
    4. const msg = useRequest(() => queryUserList(params));
    5. const deleteProducts = async (id: string) => {
    6. try {
    7. await removeProducts(id);
    8. message.success('success');
    9. msg.run();
    10. } catch (error) {
    11. message.error('fail');
    12. }
    13. };
    14. return {
    15. dataSource: msg.data,
    16. loading: msg.loading,
    17. deleteProducts,
    18. };

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

    1. import { useModel } from 'umi';
    2. import ProductList from '@/components/ProductList';
    3. const Products = () => {
    4. const { dataSource, reload, deleteProducts } = useModel('useProductList');
    5. return (
    6. <div>
    7. <a onClick={() => reload()}>reload</a>
    8. <ProductList onDelete={deleteProducts} products={dataSource} />
    9. </div>
    10. );
    11. };
    12. export default Products;

    执行启动命令:

    访问 ,应该能看到以下效果:

    一个标准的中后台页面,一般都需要一个布局,这个布局很多时候都是高度雷同的,ProLayout 封装了常用的菜单,面包屑,页头等功能,提供了一个不依赖的框架且开箱即用的高级布局组件。

    并且支持 side, mix, top 三种模式,更是内置了菜单选中,菜单生成面包屑,自动设置页面标题的逻辑。可以帮助你快速的开始一个项目。

    site

    使用方式也是极为简单,只需要进行几个简单的设置。

    1. import { Button } from 'antd';
    2. import ProLayout, { PageContainer } from '@ant-design/pro-layout';
    3. export default (
    4. <ProLayout>
    5. <PageContainer
    6. extra={[
    7. <Button key="3">Operating</Button>,
    8. <Button key="2">Operating</Button>,
    9. <Button key="1" type="primary">
    10. Main Operating
    11. </Button>,
    12. ]}
    13. footer={[<Button>reset</Button>, <Button type="primary">submit</Button>]}
    14. >
    15. {children}
    16. </PageContainer>
    17. </ProLayout>
    18. );

    点击这里快速开始

    ProTable

    一个中后台页面中很多数据都不需要跨页面共享,models 在一些时候也是不需要的。

    1. import ProTable from '@ant-design/pro-table';
    2. import { queryProductList } from '@/services/product';
    3. const Products = () => {
    4. const actionRef = useRef<ActionType>();
    5. const deleteProducts = async (id: string) => {
    6. try {
    7. await removeProducts(id);
    8. actionRef.current?.reload();
    9. } catch (error) {
    10. message.error('fail');
    11. }
    12. };
    13. const columns = [
    14. {
    15. title: 'Name',
    16. dataIndex: 'name',
    17. },
    18. {
    19. title: 'Actions',
    20. render: (text, record) => {
    21. return (
    22. <Popconfirm title="Delete?" onConfirm={() => onDelete(record.id)}>
    23. <Button>Delete</Button>
    24. </Popconfirm>
    25. );
    26. },
    27. },
    28. ];
    29. return (
    30. <ProTable<{ name: string }>
    31. headerTitle="查询表格"
    32. actionRef={actionRef}
    33. rowKey="name"
    34. request={(params, sorter, filter) => queryProductList({ ...params, sorter, filter })}
    35. columns={columns}
    36. />
    37. );
    38. };

    ProTable 提供了预设逻辑来处理 loading,分页 和搜索表单,可以大大减少代码量,点击这里。

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

    下一步

    我们已经完成了一个简单应用,你可能还有很多疑问,比如:

    • 如何统一处理出错?

    • 如何处理更多路由,比如动态路由,嵌套路由,权限路由等?

    • 如何 mock 数据?

    • 如何部署?

    • 等等

    你可以: