umi路由跳转问题,监控

本文探讨了在umi前端框架中遇到的路由跳转问题,特别是如何在跳转时保持页面缓存,并在返回时恢复筛选条件和页码。解决方案包括使用hooks监听props.history.location变化以及利用Umi的history.block和Prompt API实现页面离开提醒。

这里写自定义目录标题

umi路由跳转问题

  1. 描述一下问题,有一个需求就是要记录页面缓存,跳到其他页面的时候,当通过goBack、或者是history.go(-1)返回的时候,保存当前页面的筛选条件,以及页码。
  2. 或者要离开当前页面的时候,需要做一下提醒。

解决方式

  1. hooks,监听当前页面的props.history.location变化
useEffect(() => {
    if (props.history.location.pathname === '/O2OBusinessManger' && props.history.location.action === 'POP') {
          console.log('执行');
        }
  }, [props.history.location]);
  1. Umi history.block 的实现
history.block((location, action) => {
    if (location.pathname === '/O2OBusinessManger' && action === 'POP') {
      console.log('提示');
    }
  })

参考:history.block

  1. Umi API Prompt (当页面离开提示)
import { Prompt } from 'umi';

export default () => {
  return (
    <div>
      <Prompt message="你确定要离开么?" />
    </div>
  );
};

写一篇有用的文章不容易欢迎打赏与讨论
在这里插入图片描述

umi 中实现路由权限可以通过以下几个步骤: 1.路由配置文件中定义路由时,可以添加一个 `authority` 属性来标识该路由需要的权限。比如: ``` routes: [ { path: '/', component: '@/pages/index', authority: ['admin', 'user'] }, ... ] ``` 2. 在应用程序中,可以通过一些方式获取当前用户的权限列表。比如从后端接口获取、从浏览器缓存中获取等等。 3. 在应用程序中,可以编写一个 `Authorized` 组件来控制路由是否可以渲染。该组件可以接收一个权限列表作为参数,然后根据当前用户的权限列表和路由配置中的权限要求,来判断当前路由是否可以渲染。比如: ``` import React from 'react'; import { Redirect, Route } from 'umi'; const Authorized = ({ children, authority }) => { // 获取当前用户的权限列表 const currentUserAuthority = ['admin', 'user']; // 判断当前路由是否可以渲染 if (authority.some(item => currentUserAuthority.includes(item))) { return children; } else { return <Redirect to="/403" />; } }; export default Authorized; ``` 4.路由配置文件中使用 `Authorized` 组件来包装需要进行权限控制的路由即可。比如: ``` routes: [ { path: '/', component: '@/layouts/index', routes: [ { path: '/', component: '@/pages/index', authority: ['admin', 'user'], wrapper: Authorized }, ... ] }, ... ] ``` 这样,只有当当前用户的权限列表中包含该路由需要的权限时,该路由才会被渲染出来。否则,用户将被重定向到 403 页面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值