mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-27 12:39:49 +08:00
1 line
18 KiB
JavaScript
1 line
18 KiB
JavaScript
(("undefined"!=typeof globalThis?globalThis:self).makoChunk_antd=("undefined"!=typeof globalThis?globalThis:self).makoChunk_antd||[]).push([["5ad67059"],{"5ad67059":function(e,t,a){"use strict";a.d(t,"__esModule",{value:!0}),a.d(t,"texts",{enumerable:!0,get:function(){return n;}}),a("97ed4ee5");let n=[{value:"In real project development, besides UI libraries like Ant Design, you may also need build tools, routing solutions, CSS solutions, data flow solutions, request libraries and request solutions, i18n solutions, permission solutions, Icons solutions, etc. We have launched ",paraId:0},{value:"Umi",paraId:0},{value:", an enterprise application framework based on React, based on the scenarios of business scenarios, which we recommend you to use in your projects.",paraId:0},{value:"Umi is a scalable enterprise front-end application framework and the underlying front-end framework of Ant Group, which has served 10,000+ applications directly or indirectly.",paraId:1},{value:"This article will guide you through creating a simple application from scratch using Umi, Ant Design and ",paraId:2},{value:"Ant Design Pro",paraId:2},{value:".",paraId:2},{value:"The recommended way to create a Umi scaffold is using ",paraId:3,tocIndex:0},{value:"pnpm",paraId:3,tocIndex:0},{value:" to execute the following command.",paraId:3,tocIndex:0},{value:"$ mkdir myapp && cd myapp\n$ pnpm create umi\n",paraId:4,tocIndex:0},{value:"If you use npm, you can run ",paraId:5,tocIndex:0},{value:"npm create umi",paraId:5,tocIndex:0},{value:" for the same effect; if you use yarn, run ",paraId:5,tocIndex:0},{value:"yarn create umi",paraId:5,tocIndex:0},{value:"; if you use bun, which means you are a very hipster, you can run ",paraId:5,tocIndex:0},{value:"bunx create-umi",paraId:5,tocIndex:0},{value:" (note that there is a ",paraId:5,tocIndex:0},{value:"-",paraId:5,tocIndex:0},{value:" between create and umi).",paraId:5,tocIndex:0},{value:'Select "Simple App" here, because we want to start from "scratch".',paraId:6,tocIndex:0},{value:"? Pick Umi App Template \u203A - Use arrow-keys. Return to submit.\n\u276F Simple App\n Ant Design Pro\n Vue Simple App\n",paraId:7,tocIndex:0},{value:'Here we recommend "pnpm". pnpm is better in speed and handling ghost dependencies.',paraId:8,tocIndex:0},{value:"? Pick Npm Client \u203A - Use arrow-keys. Return to submit.\n npm\n cnpm\n tnpm\n yarn\n\u276F pnpm\n",paraId:9,tocIndex:0},{value:'For those in China, we recommend choosing "taobao", otherwise choose "npm".',paraId:10,tocIndex:0},{value:"? Pick Npm Registry \u203A - Use arrow-keys. Return to submit.\n\u276F npm\n taobao\n",paraId:11,tocIndex:0},{value:"The tool then automatically installs the dependencies and executes the initialization script for the umi.",paraId:12,tocIndex:0},{value:"Before starting the project, let's install some more dependencies that will be used in this tutorial.",paraId:13,tocIndex:0},{value:"$ pnpm i @umijs/plugins -D\n$ pnpm i antd axios @ant-design/pro-components -S\n",paraId:14,tocIndex:0},{value:"@umijs/plugins",paraId:15,tocIndex:0},{value:" is the official plugin set of Umi, containing a large number of plugins such as valtio, react-query, styled-components, locale, access, qiankun, etc. ",paraId:15,tocIndex:0},{value:"antd",paraId:15,tocIndex:0},{value:" needs no introduction. ",paraId:15,tocIndex:0},{value:"axios",paraId:15,tocIndex:0},{value:" is the request library; ",paraId:15,tocIndex:0},{value:"@ant-design/pro-components",paraId:15,tocIndex:0},{value:" is the component used to generate the layouts.",paraId:15,tocIndex:0},{value:"When finished, execute the following command to start the project.",paraId:16,tocIndex:0},{value:"$ npm run dev\numi dev\ninfo - Umi v4.0.46\n \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\n \u2551 App listening at: \u2551\n \u2551 > Local: http://localhost:8000 \u2551\nready - \u2551 > Network: http://*********:8000 \u2551\n \u2551 \u2551\n \u2551 Now you can open browser with the above addresses\u2191 \u2551\n \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\n",paraId:17,tocIndex:0},{value:"Follow the prompts and click the url in the command line, which will automatically open the browser. If it goes well, you will see the following screen.",paraId:18,tocIndex:0},{value:"We're going to write an application to display a list of products. The first step is to create the routes, which can be thought of as the different pages that make up the application. Umi users don't usually need to care about the implementation behind Umi, but in case you're wondering, Umi's routes are based on react-router@6.3 (Note: not the latest 6.4, which contains loader and action functionality that is not required for Umi).",paraId:19,tocIndex:1},{value:"We can create routes with cli.",paraId:20,tocIndex:1},{value:"$ npx umi g page products\nWrite: src/pages/products.tsx\nWrite: src/pages/products.less\n",paraId:21,tocIndex:1},{value:"Then modify the configuration file ",paraId:22,tocIndex:1},{value:".umirc.ts",paraId:22,tocIndex:1},{value:" with the new route declaration.",paraId:22,tocIndex:1},{value:'import { defineConfig } from "umi";\n\nexport default defineConfig({\n routes: [\n { path: "/", component: "index" },\n { path: "/docs", component: "docs" },\n+ { path: "/products", component: "products" },\n ],\n npmClient: "pnpm",\n});\n',paraId:23,tocIndex:1},{value:"Since the boilerplate uses configured routing, as the name implies, the routes are configured line by line by people, which is tedious but more flexible, this way you need to add the routes field to the configuration, see ",paraId:24,tocIndex:1},{value:"Umi Documentation on Routing",paraId:24,tocIndex:1},{value:". In addition, Umi also supports protocol-based routing, meaning that the file system is the route, so there is no need to configure routes to take effect.",paraId:24,tocIndex:1},{value:"Then we edit the ",paraId:25,tocIndex:1},{value:"src/layouts/index.tsx",paraId:25,tocIndex:1},{value:" file and add the navigation to the ",paraId:25,tocIndex:1},{value:"/products",paraId:25,tocIndex:1},{value:" path in the global layout route.",paraId:25,tocIndex:1},{value:'<li>\n <Link to="/docs">Docs</Link>\n</li>\n+ <li>\n+ <Link to="/products">Products</Link>\n+ </li>\n',paraId:26,tocIndex:1},{value:"Open ",paraId:27,tocIndex:1},{value:"http://localhost:8000/products",paraId:27,tocIndex:1},{value:" and if it goes well, you will see the following page.",paraId:27,tocIndex:1},{value:"As your application grows, you'll need to share UI elements across multiple pages (or use them multiple times on a single page), and in Umi you can abstract this out into components. Let's write a ProductList component so that we can display the product list in different places.",paraId:28,tocIndex:2},{value:"Create a new ",paraId:29,tocIndex:2},{value:"src/components/ProductList.tsx",paraId:29,tocIndex:2},{value:" file with the following code.",paraId:29,tocIndex:2},{value:"import React from 'react';\nimport { Button, Popconfirm, Table } from 'antd';\nimport type { TableProps } from 'antd';\n\ninterface DataType {\n id: string;\n name: string;\n}\n\nconst ProductList: React.FC<{ products: DataType[]; onDelete: (id: string) => void }> = ({\n onDelete,\n products,\n}) => {\n const columns: TableProps<DataType>['columns'] = [\n {\n title: 'Name',\n dataIndex: 'name',\n },\n {\n title: 'Actions',\n render(text, record) {\n return (\n <Popconfirm title=\"Delete?\" onConfirm={() => onDelete(record.id)}>\n <Button>Delete</Button>\n </Popconfirm>\n );\n },\n },\n ];\n return <Table rowKey=\"id\" dataSource={products} columns={columns} />;\n};\n\nexport default ProductList;\n",paraId:30,tocIndex:2},{value:"Assuming we have agreed on an API interface with the backend developers, we can now use Mock data to locally mock up the data that the API should return, so that front-end and back-end development can proceed simultaneously without the front-end work being blocked because the back-end API is still being developed. Umi provides an out-of-the-box ",paraId:31,tocIndex:3},{value:"Mock function",paraId:31,tocIndex:3},{value:" that allows you to set up Mock data in a convenient and easy way.",paraId:31,tocIndex:3},{value:"Create a new ",paraId:32,tocIndex:3},{value:"mock/products.ts",paraId:32,tocIndex:3},{value:" file in the root directory with the following code.",paraId:32,tocIndex:3},{value:"import { defineMock } from 'umi';\n\ntype Product = {\n id: string;\n name: string;\n};\n\nlet products: Product[] = [\n { id: '1', name: 'Umi' },\n { id: '2', name: 'Ant Design' },\n { id: '3', name: 'Ant Design Pro' },\n { id: '4', name: 'Dva' },\n];\n\nexport default defineMock({\n 'GET /api/products': (_, res) => {\n res.send({\n status: 'ok',\n data: products,\n });\n },\n 'DELETE /api/products/:id': (req, res) => {\n products = products.filter((item) => item.id !== req.params.id);\n res.send({ status: 'ok' });\n },\n});\n",paraId:33,tocIndex:3},{value:"Then visit ",paraId:34,tocIndex:3},{value:"http://localhost:8000/api/products",paraId:34,tocIndex:3},{value:" and you will see the api response.",paraId:34,tocIndex:3},{value:"With the UI components and Mock data done, it's time to bring them together. The request solution is needed here, and our choice here is react-query (if you want to say @tanstack/react-query, yes, they are the same library, and @tanstack/react-query is a renamed package of react-query). So before you start, you need to change the configuration to enable the ",paraId:35,tocIndex:4},{value:"react-query plugin for Umi",paraId:35,tocIndex:4},{value:" with one click.",paraId:35,tocIndex:4},{value:"First edit ",paraId:36,tocIndex:4},{value:".umirc.ts",paraId:36,tocIndex:4},{value:".",paraId:36,tocIndex:4},{value:'import { defineConfig } from "umi";\n\nexport default defineConfig({\n+ plugins: [\'@umijs/plugins/dist/react-query\'],\n+ reactQuery: {},\n routes: [\n { path: "/", component: "index" },\n { path: "/docs", component: "docs" },\n { path: "/products", component: "products" },\n ],\n npmClient: \'pnpm\',\n});\n',paraId:37,tocIndex:4},{value:"Edit ",paraId:38,tocIndex:4},{value:"src/pages/products.tsx",paraId:38,tocIndex:4},{value:" with the following code.",paraId:38,tocIndex:4},{value:"import React from 'react';\nimport axios from 'axios';\nimport { useMutation, useQuery, useQueryClient } from 'umi';\n\nimport styles from './products.less';\nimport ProductList from '@/components/ProductList';\n\nexport default function Page() {\n const queryClient = useQueryClient();\n const productsQuery = useQuery(['products'], {\n queryFn() {\n return axios.get('/api/products').then((res) => res.data);\n },\n });\n const productsDeleteMutation = useMutation({\n mutationFn(id: string) {\n return axios.delete(`/api/products/${id}`);\n },\n onSettled: () => {\n queryClient.invalidateQueries({ queryKey: ['products'] });\n },\n });\n if (productsQuery.isLoading) return null;\n return (\n <div>\n <h1 className={styles.title}>Page products</h1>\n <ProductList\n products={productsQuery.data.data}\n onDelete={(id) => {\n productsDeleteMutation.mutate(id);\n }}\n />\n </div>\n );\n}\n",paraId:39,tocIndex:4},{value:"Here, we pull the data from ",paraId:40,tocIndex:4},{value:"/api/products",paraId:40,tocIndex:4},{value:" with ",paraId:40,tocIndex:4},{value:"useQuery()",paraId:40,tocIndex:4},{value:" and submit a DELETE request to ",paraId:40,tocIndex:4},{value:"/api/products/${id}",paraId:40,tocIndex:4},{value:" in the ",paraId:40,tocIndex:4},{value:"onDelete",paraId:40,tocIndex:4},{value:" event with ",paraId:40,tocIndex:4},{value:"useMutation()",paraId:40,tocIndex:4},{value:" to perform the delete operation. For more details on the use of react-query, please refer to ",paraId:40,tocIndex:4},{value:"Umi Plugin for React Query",paraId:40,tocIndex:4},{value:" and ",paraId:40,tocIndex:4},{value:"React Query Official Website",paraId:40,tocIndex:4},{value:".",paraId:40,tocIndex:4},{value:"After saving, you should see the following screen.",paraId:41,tocIndex:4},{value:"A standard backend project generally need a layout, this layout is very often highly similar, ",paraId:42,tocIndex:5},{value:"ProLayout",paraId:42,tocIndex:5},{value:" encapsulates the common menu, breadcrumbs, page headers and other functions, provides a non-dependent framework and an out-of-the-box advanced layout component. And support ",paraId:42,tocIndex:5},{value:"side",paraId:42,tocIndex:5},{value:", ",paraId:42,tocIndex:5},{value:"mix",paraId:42,tocIndex:5},{value:", ",paraId:42,tocIndex:5},{value:"top",paraId:42,tocIndex:5},{value:" three modes, but also built-in menu selection, menu generation breadcrumbs, automatically set the logic of the page title.",paraId:42,tocIndex:5},{value:"Modify the configuration for each route to add a name field for ProLayout to do menu rendering use.",paraId:43,tocIndex:5},{value:'import { defineConfig } from "umi";\n\nexport default defineConfig({\n routes: [\n- { path: "/", component: "index" },\n+ { path: "/", component: "index", name: "home" },\n- { path: "/docs", component: "docs" },\n+ { path: "/docs", component: "docs", name: "docs" },\n- { path: "/products", component: "products" },\n+ { path: "/products", component: "products", name: "products" },\n ],\n plugins: ["@umijs/plugins/dist/react-query"],\n reactQuery: {},\n npmClient: "pnpm",\n});\n',paraId:44,tocIndex:5},{value:"Edit ",paraId:45,tocIndex:5},{value:"src/layouts/index.tsx",paraId:45,tocIndex:5},{value:" with the following code.",paraId:45,tocIndex:5},{value:"import { ProLayout } from '@ant-design/pro-components';\nimport { Link, Outlet, useAppData, useLocation } from 'umi';\n\nexport default function Layout() {\n const { clientRoutes } = useAppData();\n const location = useLocation();\n return (\n <ProLayout\n route={clientRoutes[0]}\n location={location}\n title=\"Umi x Ant Design\"\n menuItemRender={(menuItemProps, defaultDom) => {\n if (menuItemProps.isUrl || menuItemProps.children) {\n return defaultDom;\n }\n if (menuItemProps.path && location.pathname !== menuItemProps.path) {\n return (\n <Link to={menuItemProps.path} target={menuItemProps.target}>\n {defaultDom}\n </Link>\n );\n }\n return defaultDom;\n }}\n >\n <Outlet />\n </ProLayout>\n );\n}\n",paraId:46,tocIndex:5},{value:"Here we first use umi's ",paraId:47,tocIndex:5},{value:"useAppData",paraId:47,tocIndex:5},{value:" to get the global client route ",paraId:47,tocIndex:5},{value:"clientRoutes",paraId:47,tocIndex:5},{value:", which is a nested routing object, and we pass ",paraId:47,tocIndex:5},{value:"clientRoutes[0]",paraId:47,tocIndex:5},{value:" to ProLayout; then we use ",paraId:47,tocIndex:5},{value:"useLocation()",paraId:47,tocIndex:5},{value:" to get the location information, which is also passed to ProLayout to decide which menu should be highlighted; we also want to do a route jump when we click on the menu, so we need to customize ProLayout's menuItemRender method.",paraId:47,tocIndex:5},{value:"You may have found ",paraId:48,tocIndex:5},{value:"src/layouts/index.less",paraId:48,tocIndex:5},{value:" has no place to refer to him, in order to keep the project file tidy, you can choose to delete him.",paraId:48,tocIndex:5},{value:"The browser will automatically refresh at this point, and if it goes well, you'll see the following screen.",paraId:49,tocIndex:5},{value:"After completing the development and verifying it in the development environment, it is time to deploy it to our users by executing the following command.",paraId:50,tocIndex:6},{value:"$ npm run build\ninfo - Umi v4.0.46\n\u2714 Webpack\n Compiled successfully in 5.31s\ninfo - File sizes after gzip:\n 122.45 kB dist/umi.js\n 575 B dist/src__pages__products.async.js\n 312 B dist/src__pages__index.async.js\n 291 B dist/layouts__index.async.js\n 100 B dist/layouts__index.chunk.css\n 55 B dist/src__pages__products.chunk.css\nevent - Build index.html\n",paraId:51,tocIndex:6},{value:"The build command will package all resources, including JavaScript, CSS, Web Fonts, images, Html, etc. You can find these files in the ",paraId:52,tocIndex:6},{value:"dist/",paraId:52,tocIndex:6},{value:" directory.",paraId:52,tocIndex:6},{value:"We have completed a simple application and you may still have many questions, such as",paraId:53,tocIndex:7},{value:"How to handle errors uniformly?",paraId:54,tocIndex:7},{value:"How to handle more routing, such as dynamic routing, nested routing, permission routing, etc.?",paraId:54,tocIndex:7},{value:"How to use a data flow scheme?",paraId:54,tocIndex:7},{value:"How to modify webpack configuration or switch to vite build mode?",paraId:54,tocIndex:7},{value:"etc.",paraId:54,tocIndex:7},{value:"You can.",paraId:55,tocIndex:7},{value:"Visit ",paraId:56,tocIndex:7},{value:"Umi official website",paraId:56,tocIndex:7},{value:"Learn about ",paraId:56,tocIndex:7},{value:"Umi's Routing",paraId:56,tocIndex:7},{value:"Learn about ",paraId:56,tocIndex:7},{value:"Umi Max",paraId:56,tocIndex:7},{value:", which is more integrated than Umi",paraId:56,tocIndex:7},{value:"Learn about the out-of-the-box middle and backend scaffolding ",paraId:56,tocIndex:7},{value:"Ant Design Pro",paraId:56,tocIndex:7},{value:"Learn about advanced layouts ",paraId:56,tocIndex:7},{value:"ProLayout",paraId:56,tocIndex:7},{value:"Learn about advanced tables ",paraId:56,tocIndex:7},{value:"ProTable",paraId:56,tocIndex:7}];}}]); |