博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于React ant design 动态路由根据url的动态id,分别不同的页面,并设置默认跳转页面
阅读量:4098 次
发布时间:2019-05-25

本文共 2967 字,大约阅读时间需要 9 分钟。

  • 效果图在这里插入图片描述

法一

获取params,然后进入“/account/company/:id”时获取到的parmas的id为“:id”,在js中判断是否为字符串":id",若是history.replace跳转到对应的"/account/company/index"页面

缺点:url会出现闪跳

  • config.ts 部分代码
{
name: 'account', icon: 'user', path: '/account', routes: [ {
name: 'center', icon: 'TeamOutlined', path: '/account/company/:id', component: './account/company', }, ]}
  • js代码(可放入componentDidMount中)
const {
dispatch, match: {
params: {
id}}} = this.props;if (id === ':id') {
history.replace('/account/company/index'); return}

法二

  1. 将history.replace相关代码删掉;
  2. “/account/company/index”添加到config.ts中
{
name: 'center', icon: 'TeamOutlined', hideInMenu: true, path: '/account/company/:id', component: './account/company'},{
name: 'center', icon: 'TeamOutlined', path: '/account/company/index', component: './account/company', },}
  • Component.page
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 (
this.selectKey(key as RouterKeys)} > {
this.getMenu()}
); }}export default Center;

转载地址:http://ifqii.baihongyu.com/

你可能感兴趣的文章
C++静态成员函数访问非静态成员的几种方法
查看>>
类中的静态成员函数访问非静态成员变量
查看>>
C++学习之普通函数指针与成员函数指针
查看>>
C++的静态成员函数指针
查看>>
Linux系统编程——线程池
查看>>
yfan.qiu linux硬链接与软链接
查看>>
Linux C++线程池实例
查看>>
shared_ptr简介以及常见问题
查看>>
c++11 你需要知道这些就够了
查看>>
c++11 你需要知道这些就够了
查看>>
shared_ptr的一些尴尬
查看>>
C++总结8——shared_ptr和weak_ptr智能指针
查看>>
c++写时拷贝1
查看>>
C++ 写时拷贝 2
查看>>
Linux网络编程---I/O复用模型之poll
查看>>
Java NIO详解
查看>>
单列模式-编写类ConfigManager读取属性文件
查看>>
java中float和double的区别
查看>>
Statement与PreparedStatement区别
查看>>
Tomcat配置数据源步骤以及使用JNDI
查看>>