11 changed files with 1371 additions and 58 deletions
@ -0,0 +1,11 @@ |
|||||
|
{ |
||||
|
"tabWidth": 2, |
||||
|
"useTabs": false, |
||||
|
"semi": false, |
||||
|
"singleQuote": false, |
||||
|
"TrailingCooma": "all", |
||||
|
"bracketSpacing": true, |
||||
|
"jsxBracketSameLine": false, |
||||
|
"arrowParens": "avoid", |
||||
|
"printWidth": 140 |
||||
|
} |
File diff suppressed because it is too large
@ -1,41 +1,54 @@ |
|||||
import { |
import { BrowserRouter as Router, Switch, Redirect, Route, Link } from "react-router-dom" |
||||
BrowserRouter as Router, |
import React, { Fragment } from "react" |
||||
Switch, |
|
||||
Redirect, |
|
||||
Route, |
|
||||
Link, |
|
||||
} from "react-router-dom"; |
|
||||
import React from "react"; |
|
||||
|
|
||||
import { pageList } from "@/plugins/pageHoc"; |
import { pageList } from "@/plugins/pageHoc" |
||||
|
|
||||
import Layout from "@/views/Layout"; |
import Layout from "@/views/Layout" |
||||
|
|
||||
import Home from "@/views/Home"; |
import Auth from "@/ui/Auth" |
||||
import About from "@/views/About"; |
import routes from "./route" |
||||
|
|
||||
|
function RouteMap(props: any) { |
||||
|
const routes: any[] = props.routes |
||||
|
return ( |
||||
|
<Switch> |
||||
|
{routes.map((route, index) => { |
||||
|
console.log(route.path, route.exact) |
||||
|
|
||||
|
const { exact = false } = route |
||||
|
if (route.redirect) { |
||||
|
return ( |
||||
|
<Route key={index} path={route.path} exact={exact}> |
||||
|
<Redirect to={route.redirect}></Redirect> |
||||
|
</Route> |
||||
|
) |
||||
|
} |
||||
|
if (route.component) { |
||||
|
return ( |
||||
|
<Auth key={index} needAuth={!!route.meta?.auth} path={route.path} exact={exact}> |
||||
|
<route.component>{route.children && <RouteMap routes={route.children}></RouteMap>}</route.component> |
||||
|
</Auth> |
||||
|
) |
||||
|
} |
||||
|
})} |
||||
|
</Switch> |
||||
|
) |
||||
|
} |
||||
|
|
||||
export default function () { |
export default function () { |
||||
console.log(pageList); |
console.log(pageList) |
||||
|
|
||||
return ( |
return ( |
||||
<Router> |
<Router> |
||||
|
{/* <Switch> */} |
||||
<Layout |
<Layout |
||||
render={() => { |
render={() => { |
||||
return ( |
return ( |
||||
<Switch> |
<RouteMap routes={routes}></RouteMap> |
||||
<Route path="/about"> |
) |
||||
<About /> |
|
||||
</Route> |
|
||||
<Route path="/home"> |
|
||||
<Home /> |
|
||||
</Route> |
|
||||
<Route path="/"> |
|
||||
<Redirect to="/home"></Redirect> |
|
||||
</Route> |
|
||||
</Switch> |
|
||||
); |
|
||||
}} |
}} |
||||
></Layout> |
></Layout> |
||||
|
{/* </Switch> */} |
||||
</Router> |
</Router> |
||||
); |
) |
||||
} |
} |
||||
|
@ -1 +1,14 @@ |
|||||
$color: green; |
$color: green; |
||||
|
|
||||
|
|
||||
|
@mixin clearfix { |
||||
|
&:after { |
||||
|
clear: both; |
||||
|
content: "."; |
||||
|
display: block; |
||||
|
height: 0; |
||||
|
line-height: 0; |
||||
|
overflow: hidden; |
||||
|
} |
||||
|
*height: 1%; |
||||
|
} |
||||
|
@ -0,0 +1,45 @@ |
|||||
|
import Home from "@/views/Home" |
||||
|
import About from "@/views/About" |
||||
|
import Page404 from "@/views/Auth/Page404" |
||||
|
import React from "react" |
||||
|
|
||||
|
function Test() { |
||||
|
return <div>TExtas</div> |
||||
|
} |
||||
|
|
||||
|
export default [ |
||||
|
{ |
||||
|
path: "/about", |
||||
|
component: About, |
||||
|
exact: false, |
||||
|
meta: { |
||||
|
auth: true, |
||||
|
}, |
||||
|
children: [ |
||||
|
{ |
||||
|
path: "/about/test", |
||||
|
component: Test, |
||||
|
exact: false, |
||||
|
}, |
||||
|
], |
||||
|
}, |
||||
|
{ |
||||
|
path: "/home", |
||||
|
component: Home, |
||||
|
exact: true, |
||||
|
meta: { |
||||
|
auth: false, |
||||
|
}, |
||||
|
children: [], |
||||
|
}, |
||||
|
{ |
||||
|
path: "/", |
||||
|
exact: true, |
||||
|
redirect: "/home", |
||||
|
}, |
||||
|
{ |
||||
|
path: "*", |
||||
|
exact: false, |
||||
|
component: Page404, |
||||
|
}, |
||||
|
] |
@ -0,0 +1,37 @@ |
|||||
|
import React, { Component, ReactElement } from "react" |
||||
|
// 高阶组件,就是对原来的组件进行封装
|
||||
|
import { Route, Redirect, NavLink } from "react-router-dom" |
||||
|
|
||||
|
interface IProps { |
||||
|
needAuth?: boolean |
||||
|
path: string |
||||
|
exact?: boolean |
||||
|
} |
||||
|
|
||||
|
class Auth extends Component<IProps> { |
||||
|
constructor(props: IProps) { |
||||
|
super(props) |
||||
|
} |
||||
|
|
||||
|
render() { |
||||
|
const { path, needAuth = false, exact = false } = this.props // 结构语法的概
|
||||
|
if (!needAuth) { |
||||
|
return ( |
||||
|
// 如果已经登陆,就直接显示原来的组件
|
||||
|
<Route exact={exact} path={path}> |
||||
|
{this.props.children} |
||||
|
</Route> |
||||
|
) |
||||
|
} else { |
||||
|
return ( |
||||
|
<div> |
||||
|
<NavLink to="/login">你还有没有登陆好吧!</NavLink> |
||||
|
|
||||
|
{/* <Redirect to={{ pathname: '/login', state: { from: { path }} }} > </Redirect> */} |
||||
|
</div> |
||||
|
) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export default Auth |
@ -1,9 +1,13 @@ |
|||||
import React from "react"; |
import React from "react" |
||||
import { useLocation } from "react-router-dom"; |
import { useLocation } from "react-router-dom" |
||||
|
|
||||
export default function (props: any) { |
export default function (props: any) { |
||||
|
|
||||
let location = useLocation() |
let location = useLocation() |
||||
console.log(location); |
console.log(location) |
||||
return <div className="container mx-auto">About</div>; |
return ( |
||||
|
<div className="container mx-auto"> |
||||
|
<div>About</div> |
||||
|
<div>22{props.children}</div> |
||||
|
</div> |
||||
|
) |
||||
} |
} |
||||
|
@ -0,0 +1,10 @@ |
|||||
|
import React from "react" |
||||
|
import { NavLink } from "react-router-dom" |
||||
|
|
||||
|
export default function () { |
||||
|
return ( |
||||
|
<div> |
||||
|
<NavLink to="/">404</NavLink> |
||||
|
</div> |
||||
|
) |
||||
|
} |
@ -1,10 +1,16 @@ |
|||||
import React from "react"; |
import React, { ReactElement } from "react" |
||||
import { useLocation } from "react-router-dom"; |
import { useLocation } from "react-router-dom" |
||||
|
|
||||
export default function (props: any) { |
interface IProps { |
||||
|
render(): ReactElement |
||||
|
} |
||||
|
|
||||
|
export default function (props: IProps) { |
||||
return ( |
return ( |
||||
<div> |
<div> |
||||
<div className="content">{props?.render()}</div> |
<div className="content"> |
||||
|
<props.render></props.render> |
||||
|
</div> |
||||
</div> |
</div> |
||||
); |
) |
||||
} |
} |
||||
|
Loading…
Reference in new issue