|
@@ -4,12 +4,13 @@ import { NavigationGuardsProps, RouteModel } from '@/types/router'
|
|
|
import { combinationPath } from '@/utils/util'
|
|
|
import React, { Component } from 'react'
|
|
|
import KeepAlive from 'react-activation'
|
|
|
+// import { KeepAlive } from 'react-keep-alive'
|
|
|
import { Redirect, Route } from "react-router-dom"
|
|
|
class NavigationGuards extends Component<NavigationGuardsProps, any> {
|
|
|
- // constructor(props: NavigationGuardsProps) {
|
|
|
- // super(props)
|
|
|
- // userState.check()
|
|
|
- // }
|
|
|
+ constructor(props: NavigationGuardsProps) {
|
|
|
+ super(props)
|
|
|
+ userState.check()
|
|
|
+ }
|
|
|
/**
|
|
|
* 判断pathTarget是否包涵pathConfig/
|
|
|
* 即,pathConfig是否为pathTarget的子路径
|
|
@@ -31,7 +32,6 @@ class NavigationGuards extends Component<NavigationGuardsProps, any> {
|
|
|
shouldComponentUpdate(nextProps: any) {
|
|
|
//与上次请求的路径相同时不重新渲染
|
|
|
if (this.props.location.pathname === nextProps.location.pathname) return false
|
|
|
- userState.check()
|
|
|
return true
|
|
|
}
|
|
|
|
|
@@ -55,7 +55,6 @@ class NavigationGuards extends Component<NavigationGuardsProps, any> {
|
|
|
}
|
|
|
|
|
|
render() {
|
|
|
- console.log("路由守卫渲染")
|
|
|
const { location, routeConfig, match } = this.props
|
|
|
//如果没有提供routeConfig则不做任何事情
|
|
|
if (!routeConfig || routeConfig.length <= 0) { return null }
|
|
@@ -65,7 +64,6 @@ class NavigationGuards extends Component<NavigationGuardsProps, any> {
|
|
|
//用户当前希望访问的路径
|
|
|
const targetPath: string | undefined = location?.pathname
|
|
|
|
|
|
-
|
|
|
//如果访问子菜单,则跳转到子菜单的默认路由
|
|
|
if (targetPath && frameState.defaultRouteMapping.has(targetPath)) {
|
|
|
const targetDefaultRoute: string = frameState.defaultRouteMapping.get(targetPath) as string
|
|
@@ -75,8 +73,8 @@ class NavigationGuards extends Component<NavigationGuardsProps, any> {
|
|
|
const findRes = targetPath && NavigationGuards.findTargetRoute(parentPath, targetPath, routeConfig)
|
|
|
const targetRoute: RouteModel | null | "" | undefined | Error = findRes && findRes.targetRoute
|
|
|
|
|
|
- const isLogin = userState.isLogin
|
|
|
|
|
|
+ const isLogin = userState.isLogin
|
|
|
|
|
|
if (targetRoute instanceof Error) return <ErrorPage/>
|
|
|
|
|
@@ -108,6 +106,7 @@ function LoginHandler(props: { targetRoute: RouteModel, ErrorPage: any }): any {
|
|
|
const { targetRoute, ErrorPage } = props
|
|
|
const { path, auth } = targetRoute
|
|
|
const noCache = targetRoute.meta?.noCache ? targetRoute.meta.noCache : false
|
|
|
+
|
|
|
if (path === '/login') {
|
|
|
return <Redirect to="/console/dashboard"></Redirect>
|
|
|
} else if (!auth || NavigationGuards.permissionAuthentication(auth, userState.role)) {
|
|
@@ -117,7 +116,7 @@ function LoginHandler(props: { targetRoute: RouteModel, ErrorPage: any }): any {
|
|
|
noCache ?
|
|
|
<targetRoute.component {...props} routeConfig={targetRoute.childRoutes}></targetRoute.component> :
|
|
|
(
|
|
|
- <KeepAlive id={`${path}`}>
|
|
|
+ <KeepAlive id={path}>
|
|
|
<targetRoute.component {...props} routeConfig={targetRoute.childRoutes}></targetRoute.component>
|
|
|
</KeepAlive>
|
|
|
)
|