dva+antd+ts的路由菜单布局

小鱼儿 2022-12-02 01:09 303阅读 0赞

rfce(rcc)快速创建例子

1.路由配置

routes/config.ts

  1. import React from 'react'
  2. import { HomeOutlined, OrderedListOutlined, NotificationOutlined } from '@ant-design/icons';
  3. export interface route{
  4. title?: string
  5. pathname: string
  6. component: any
  7. isTop: boolean
  8. }
  9. const routes = [
  10. {
  11. title: 'Dashboard',
  12. pathname: '/dashboard',
  13. icon: HomeOutlined,
  14. component: React.lazy(() => import('../views/dashboard')),
  15. },
  16. {
  17. title: '欢迎',
  18. pathname: '/helloworld',
  19. icon:HomeOutlined,
  20. component: React.lazy(() => import('../views/helloworld')),
  21. isTop:true
  22. },
  23. {
  24. title: '罐管理',
  25. pathname: '/tankmanage',
  26. icon:OrderedListOutlined,
  27. component: React.lazy(() => import('../views/TankManage')),
  28. isTop:true
  29. },
  30. {
  31. title: '404',
  32. pathname: '/404',
  33. component: React.lazy(() => import('../views/404')),
  34. },
  35. {
  36. title: '/',
  37. pathname: '/',
  38. component: React.lazy(() => import('../views/helloworld')),
  39. }
  40. ]
  41. export default routes

在这里插入图片描述
/routes/createRoutes.tsx

  1. import React, { Suspense } from 'react'
  2. import { Router, Route, Switch, Redirect, RouteComponentProps, RouteProps } from 'dva/router'
  3. import routes, { route} from './config'
  4. import BaseLayout from '../components/layout/BaseLayout'
  5. import { Console } from 'console'
  6. const NoMatch: React.FC<RouteComponentProps> = (props: RouteComponentProps) => {
  7. return (
  8. <div>
  9. 404..
  10. </div>
  11. )
  12. }
  13. const CreateRoutes: React.FC<RouteComponentProps> = ({ history }: RouteComponentProps) => {
  14. const generateRoute: React.FC<route> = ({
  15. pathname,
  16. component: Component
  17. }: RouteProps & route) => (
  18. <Route
  19. key={ pathname}
  20. path={ pathname}
  21. exact
  22. component={ (props: RouteProps) => (
  23. <Suspense fallback={ <div />}>
  24. <Component { ...props} />
  25. </Suspense>
  26. )}
  27. />
  28. )
  29. return (
  30. <Router>
  31. '基础布局
  32. <BaseLayout>
  33. <Switch>
  34. { routes.map(({ pathname, component }: route) => generateRoute({ pathname, component }))}
  35. <Redirect to='/404'></Redirect>
  36. </Switch>
  37. </BaseLayout>
  38. </Router>
  39. )
  40. }
  41. export default CreateRoutes

在这里插入图片描述

2.引入布局

/layout/BaseLayout
在这里插入图片描述

在这里插入图片描述
加上this.props.children
在这里插入图片描述

在这里插入图片描述

3.react-loadable路由懒加载

在这里插入图片描述

4.给菜单加上链接

在这里插入图片描述
遍历动态渲染Menu
在这里插入图片描述
push
在这里插入图片描述
别忘了加上@withRouter

原教程:https://www.bilibili.com/video/BV1CE411c7i9?p=12

发表评论

表情:
评论列表 (有 0 条评论,303人围观)

还没有评论,来说两句吧...

相关阅读

    相关 IP__静态

    .静态路由的优缺点:   优点:对于路由器的CPU没有管理性开销,它意味着如果你不使用动态路由选择的话,你可能应该购买更为便宜的路由器。在路由器之间没有带宽占用,它意味...