本文共 2967 字,大约阅读时间需要 9 分钟。
获取params,然后进入“/account/company/:id”时获取到的parmas的id为“:id”,在js中判断是否为字符串":id",若是history.replace跳转到对应的"/account/company/index"页面
缺点:url会出现闪跳
{ name: 'account', icon: 'user', path: '/account', routes: [ { name: 'center', icon: 'TeamOutlined', path: '/account/company/:id', component: './account/company', }, ]}
const { dispatch, match: { params: { id}}} = this.props;if (id === ':id') { history.replace('/account/company/index'); return}
{ name: 'center', icon: 'TeamOutlined', hideInMenu: true, path: '/account/company/:id', component: './account/company'},{ name: 'center', icon: 'TeamOutlined', path: '/account/company/index', component: './account/company', },}
import { Card, Menu} from 'antd';import { Route, Switch} from 'react-router-dom';import React, { Component} from 'react';import { GridContent} from '@ant-design/pro-layout';import { FormattedMessage, history} from 'umi';import Certificate from './components/Certificate';import RoleManage from './components/RoleManage';import AuthorMange from './components/AuthorMange';import WorkFlow from './components/WorkFlow';import Detail from './components/Information';const { Item} = Menu;interface CenterProps { match: { params: { id: string; }; };}interface CenterStates { tabKey: string; menuMap: { index: any; certificate: any; 'role-manage': any; 'author-mange': any; 'account-manage': any; workflow: any; };}type RouterKeys = | 'detail' | 'certificate' | 'roleManage' | 'authorMange' | 'accountManage' | 'workflow';class Center extends Component{ state: CenterStates = { tabKey: 'index', // 这是tab列表 menuMap: { index: , certificate: , 'role-manage': , 'author-mange': , 'account-manage': , workflow: , }, }; componentDidMount() { const { match: { params: { id}, }, } = this.props; if (id === ':id') { history.replace('/account/company/index'); return; } this.setState({ tabKey: id, }); } selectKey = (key: string) => { this.setState({ tabKey: key, }); history.push(`/account/company/${ key}`); }; getMenu = () => { const { menuMap} = this.state; return Object.keys(menuMap).map((item) => - { menuMap[item]}
); }; render() { const { tabKey} = this.state; return (); }}export default Center;
转载地址:http://ifqii.baihongyu.com/