add react版前端
This commit is contained in:
11
web-react/src/router/config.js
Normal file
11
web-react/src/router/config.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import Main from '../views/main'
|
||||
import Login from '../views/login'
|
||||
import Error404 from '../views/error/404'
|
||||
|
||||
var routes = [
|
||||
{ path: '/', name: 'main', component: Main, auth: true },
|
||||
{ path: '/login', name: 'login', component: Login },
|
||||
{ path: '/404', name: '404', component: Error404 }
|
||||
]
|
||||
// auth 是否需要登录
|
||||
export default routes;
|
||||
16
web-react/src/router/index.jsx
Normal file
16
web-react/src/router/index.jsx
Normal file
@@ -0,0 +1,16 @@
|
||||
import React from 'react';
|
||||
import { Switch, BrowserRouter } from 'react-router-dom'
|
||||
import NavigationGuards from './navigationGuards'
|
||||
import RouterConfig from './config'
|
||||
|
||||
const router = () => {
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<Switch>
|
||||
<NavigationGuards routerConfig={RouterConfig} />
|
||||
</Switch>
|
||||
</BrowserRouter>
|
||||
)
|
||||
}
|
||||
|
||||
export default router
|
||||
26
web-react/src/router/navigationGuards.jsx
Normal file
26
web-react/src/router/navigationGuards.jsx
Normal file
@@ -0,0 +1,26 @@
|
||||
import React, { Component } from 'react'
|
||||
import { Route, Redirect } from 'react-router-dom'
|
||||
|
||||
import { token } from '../common/token'
|
||||
|
||||
export default class navigationGuards extends Component {
|
||||
render() {
|
||||
const { routerConfig, location } = this.props
|
||||
const { pathname } = location
|
||||
|
||||
const targetRouterConfig = routerConfig.find((item) => {
|
||||
return item.path.replace(/\s*/g, '') === pathname
|
||||
})
|
||||
|
||||
if (token.value) {
|
||||
if (pathname === '/login') {
|
||||
return <Redirect to='/' push={false} />
|
||||
}
|
||||
} else if (!token.value && pathname !== '/login') {
|
||||
return <Redirect to='/login' push={false} />
|
||||
}
|
||||
|
||||
const { component } = targetRouterConfig;
|
||||
return <Route exact path={pathname} component={component} />
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user