umi 约定式路由 关于 history.goBack() 的奇奇怪怪问题

本文讲述了在使用Reactumi框架和约定式路由时,从`localhost:8000/#/font`通过逻辑跳转至`localhost:8000/#/home`后,回退操作失效的问题。解决方法是在`font`页面跳转`home`时添加了1秒延迟。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景:react umi框架,约定式路由,history.type = hash。

场景:空白的浏览器路径输入 -- localhost:8000/#/font 通过font页面里逻辑跳转到http://localhost:8000/#/home localhost:8000/#/home 页面 ,然后在home页面点击history.goBack()的时候,奇奇怪怪的问题 出现了。。 奶奶个腿居然给我退到了空白页面。。我要的是返回font页面呀!

20220414103257

↓↓↓ font页面 ↓↓↓

import React from 'react';
import { history } from 'umi';
import styles from './FontPage.less';

const FontPage = () => {
  const onClick = () => {
    history.push('/home');
  };
  onClick();
  return <div className={`${styles.root}`}>This is FontPage.</div>;
};

export default FontPage;

↓↓↓ home页面 ↓↓↓

import React from 'react';
import styles from './HomePage.less';
import { history } from 'umi';

const HomePage = () => {
  return (
    <div className={`${styles.root}`}>
      This is HomePage.
      <h1
        onClick={() => {
          history.goBack();
        }}
      >
        goBack
      </h1>
    </div>
  );
};

export default HomePage;

最终解决

font页面跳转到home页面加个 延时器。

完美解决,但是我不李姐。。

↓↓↓ font页面 ↓↓↓

import React from 'react';
import { history } from 'umi';
import styles from './FontPage.less';

const FontPage = () => {
  const onClick = () => {
    setTimeout(() => {
      history.push('/home');
    }, 1000);
  };
  onClick();
  return <div className={`${styles.root}`}>This is FontPage.</div>;
};

export default FontPage;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值