umi 路由切换动画的实现

1、环境准备

首先得有 node,并确保 node 版本是 10.13 或以上。(mac 下推荐使用 nvm 来管理 node 版本)

$ node -v
v10.13.0

推荐使用 yarn 管理 npm 依赖,并使用国内源(阿里用户使用内网源)。

# 国内源
$ npm i yarn tyarn -g
# 后面文档里的 yarn 换成 tyarn
$ tyarn -v

# 阿里内网源
$ tnpm i yarn @ali/yarn -g
# 后面文档里的 yarn 换成 ayarn
$ ayarn -v

2、搭建umi脚手架

先找个地方建个空目录

$ mkdir myapp && cd myapp

通过官方工具创建项目

$ yarn create @umijs/umi-app
# 或 npx @umijs/create-umi-app

3、补全缺失的信息

npm i

4、安装react-transition-group插件

npm i react-transition-group

5、在src目录下创建layouts文件夹,并在文件夹下创建两个文件

在index.tsx文件中复制如下代码

import React, { useState, useEffect } from 'react'
import { TransitionGroup, CSSTransition } from 'react-transition-group'
import { history as Router, withRouter } from 'umi'
import { Switch } from 'react-router'
import './index.less'
 
const routerType = {
        'POP': 'back',
        'PUSH': 'in',
        'REPLACE': 'in'
    }
 
export default withRouter(({ location, children, history }) => {
return (
   <TransitionGroup style={{height:'100%'}} className='transition_wrapper' childFactory={(child) => (
         React.cloneElement(child, { classNames: routerType[history.action] })
   )}>
      <CSSTransition key={location.pathname} appear timeout={3000}>
         <Switch location={location}>{(children as any)?.props?.children}</Switch>
      </CSSTransition>
    </TransitionGroup>
)
})

在index.less文件中复制如下代码

.in-enter-active{  // 入场的过渡状态类
  transition: all 3s;
  transform: translate(0, 0)!important;
}
 
.in-enter-done { // 入场的动画的结束状态类
    // opacity: 0.5;
    transform: translate(0, 0) !important;
}
 
.in-enter {  // 入场的动画开始状态类
  z-index: 5 !important;
  transform: translate(100%, 0);
}
 
.in-exit-active {    // 离场动画
    opacity:0;
    transition: all 3s;
    transform: translate(-100%, 0)!important;
}
 
.in-exit {  // 离场动画开始
    // transform: translate(0, 0)!important;
}
.in-exit-done { // 离场动画结束
 
}
 
// 返回动画
.back-enter-active{  // 入场的过渡状态类
    transition: all 3s;
    transform: translate(0, 0)!important;
  
  }
.back-enter-done { // 入场的动画的结束状态类
    // opacity: 0.5;
    transform: translate(0, 0) !important;
}
 
.back-enter {  // 入场的动画开始状态类
    z-index: 5 !important;
    transform: translate(-100%, 0);
}
  
.back-exit-active {    // 离场动画
    opacity:0;
    transition: all 3s;
    transform: translate(100%, 0)!important;
}
 
.back-exit {  // 离场动画开始
    // transform: translate(0, 0)!important;
}
.back-exit-done { // 离场动画结束
 
}

旋转动画

/* 入场动画过程 */
.in-enter-active {
    transition: all 3s;
    transform: translate(0, 0) !important;
}

.in-enter-done {
    transform: translate(0, 0) !important;
}

.in-enter {
    z-index: 5 !important;
    transform: rotate(180deg)
}

.in-exit-active {
    opacity: 0;
    transform: rotate(180deg)
}

/* 出场动画过程 */
.back-enter-active {
    transition: all 3s;
    transform: translate(0, 0) !important;

}

.back-enter {
    z-index: 5 !important;
    transform: translate(-100%, 0);
}

在这里插入图片描述

7、.umirc.ts文件中的配置

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值