react 路由配置表

    1. sql语句
    2. `select * from user limit ?,?`,[sou,con]
    3. //分页
    4. select * from user where name like `%kk%`
    5. //搜索
    6. select * from user
    7. //查询
    1. // router/index.js 路由配置表
    2. // 根据路由路径加载相应的路由视图组件 加载路由视图
    3. import React from 'react'
    4. // 引入路由内置组件
    5. import {Route,Switch,Redirect} from 'react-router-dom'
    6. export default function RouterView(props) {
    7. console.log(props.routes); // 通过父子组件传参获取路由配置表
    8. // 存在路由重定向 redirect
    9. let reds = props.routes.filter(item=>item.redirect)
    10. // 不存在路由重定向 component
    11. let coms = props.routes.filter(item=>item.component)
    12. return <Switch>
    13. {/* // 不存在路由重定向 */}
    14. {
    15. coms.map((item,index)=>{
    16. // 渲染路由视图组件Route 根据路由路径渲染相应得路由视图组件
    17. return <Route key={index} path={item.path} render={routerProps=>{ // routerProps 路由元信息
    18. console.log(routerProps);
    19. console.log(item);
    20. // 首页判断当前路由对象是否需要登陆权限
    21. if(item.needRequire){ // 标识 需要登陆权限
    22. console.log('需要登陆权限');
    23. // 是否处于登陆有效期 token 登陆凭证 token存在直接加载组件token不存在跳转登陆页
    24. return localStorage.getItem('token')?<item.component {...routerProps}/>:<Redirect to='/login'></Redirect>
    25. }else{
    26. if(item.children){ // 存在路由嵌套 =》递归传参
    27. return <item.component {...routerProps} routes={item.children}/>
    28. }else{ // 不存在路由嵌套
    29. return <item.component {...routerProps}/>
    30. }
    31. }
    32. }}></Route>
    33. })
    34. }
    35. {/* 存在路由重定向 */}
    36. {
    37. reds.map((item,index)=>{
    38. return <Redirect key={index} to={item.redirect}></Redirect>
    39. })
    40. }
    41. </Switch>
    42. }
    1. // router/config.js
    2. // 生成路由配置表 => 路由视图组件/路由路径
    3. // 引入一级路由视图组件
    4. import Index from '../views/Index' // 主页面
    5. import Login from '../views/Login' // 登陆页
    6. // 引入二级路由视图组件
    7. import Home from '../views/two/Home' // 首页
    8. import Rank from '../views/two/Rank' // 排行
    9. import Free from '../views/two/Free' // 免费
    10. import Sort from '../views/two/Sort' // 分类
    11. import My from '../views/two/My' // 个人
    12. const routes = [
    13. // 路由重定向对象 redirect
    14. {
    15. path:'/',
    16. redirect:'/index/home'
    17. },
    18. // 配置登陆页路由
    19. {
    20. path:'/login',
    21. component: Login
    22. },
    23. {
    24. path:'/index',
    25. component: Index,
    26. children:[
    27. {
    28. path:'/index/home',
    29. component:Home
    30. },
    31. {
    32. path:'/index/rank',
    33. component:Rank
    34. },
    35. {
    36. path:'/index/free',
    37. component: Free
    38. },
    39. {
    40. path:'/index/sort',
    41. component: Sort
    42. },
    43. {
    44. path:'/index/my',
    45. component: My,
    46. needRequire: true // 需要登陆权限 拦截路由 登陆 正常访问页面 没登陆 先跳转登陆页面
    47. }
    48. ]
    49. }
    50. ]
    51. export default routes;
    1. // react App.js
    2. import React, { Component } from 'react'
    3. import {BrowserRouter} from 'react-router-dom'
    4. // import Index from './views/Index'
    5. // import Detail from './views/Detail'
    6. //视图组件
    7. import RouterView from './router/index'
    8. //路由配置表
    9. import config from './router/config'
    10. export default class App extends Component {
    11. render() {
    12. return (
    13. <div>
    14. <BrowserRouter>
    15. <RouterView routes={config}></RouterView>
    16. </BrowserRouter>
    17. </div>
    18. )
    19. }
    20. }