In Umi, applications are single-page) applications, and the page address jumps are done on the browser side, and the server will not be re-requested for html, and html is only loaded once when the application is initialized. All pages are composed of different components. The switching of pages is actually the switching of different components. You only need to associate different routing paths with corresponding components in the configuration.
Configure through routes in the configuration file, the format is an array of routing information.
Example:
export default {routes: [{ exact: true, path: '/', component: 'index' },{ exact: true, path: '/user', component: 'user' },],}
stringConfigure path wildcards that can be understood by path-to-regexp@^1.7.0.
stringConfigure the React component path for rendering after matching location and path. It can be an absolute path or a relative path. If it is a relative path, it will be found from src/pages.
If you point to a file in the src directory, you can use either @ or ../. For example, component:'@/layouts/basic', or component:'../layouts/basic', the former is recommended.
booleantrueIndicates whether it is a strict match, that is, whether location and path exactly correspond.
Example:
export default {routes: [// Matching fails when url is /one/two{ path: '/one', exact: true },// Matching is successful when url is /one/two{ path: '/one' },{ path: '/one', exact: false },],}
Configure sub-routes, usually used when you need to add layout components to multiple paths.
Example:
export default {routes: [{ path: '/login', component: 'login' },{path: '/',component: '@/layouts/index',routes: [{ path: '/list', component: 'list' },{ path: '/admin', component: 'admin' },],},],}
Then in src/layouts/index, pass props.children to render child routes,
export default (props) => {return <div style={{ padding: 20 }}>{ props.children }</div>;}
In this way, accessing /list and /admin will bring the layout component src/layouts/index.
stringConfigure routing jump.
Example:
export default {routes: [{ exact: true, path: '/', redirect: '/list' },{ exact: true, path: '/list', component: 'list' },],}
Visiting / will jump to /list, which will be rendered by the src/pages/list file.
string[]wrappers is HOC.
For example, you can run authorization check for a specific route:
export default {routes: [{ path: '/user', component: 'user',wrappers: ['@/wrappers/auth',],},{ path: '/login', component: 'login' },]}
See below example as content of src/wrappers/auth,
import { Redirect } from 'umi'export default (props) => {const { isLogin } = useAuth();if (isLogin) {return <div>{ props.children }</div>;} else {return <Redirect to="/login" />;}}
With above configuration, user request of /user will be validated via useAuth. src/pages/user gets rendered or page redirected to /login.
stringConfigure the route title.
import { history } from 'umi';// Jump to the specified routehistory.push('/list');// Jump to the specified route with parametershistory.push('/list?a=b');history.push({pathname: '/list',query: {a: 'b',},});// Jump to the previous routehistory.goBack();
See Configuration#history for details.
Example:
import { Link } from 'umi';export default () => (<div><Link to="/users">Users Page</Link></div>);
Then click on the Users Page to jump to the /users address.
Note:
Link is only used for internal jumps of single page applications, if it is an external address jump, please use the a tagThe routing component can get the following properties through props,
params, path, url and isExactpathname, search, and querypath, exact, component, routes, etc.Example:
export default function(props) {console.log(props.route);return <div>Home Page</div>;}
With cloneElement, one time is fine (it is necessary twice for Umi 2).
import React from 'react';export default function Layout(props) {return React.Children.map(props.children, child => {return React.cloneElement(child, { foo: 'bar' });});}