zoukankan      html  css  js  c++  java
  • react react-router-config

    https://www.npmjs.com/package/react-router-config
    

      

    这是我的目录,router文件 index.js 就是我的路由文件。 assembly 是引入组件的地方,那里配了懒加载

    第一步: 安装

    npm install --save react-router-config
    

      

    第二步: 外面  index.js 引入

    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Provider } from 'react-redux'  
    import { persistStore } from 'redux-persist'   
    import { PersistGate } from 'redux-persist/lib/integration/react';
    import { renderRoutes } from "react-router-config";
    import { BrowserRouter } from "react-router-dom"; 
    import { store } from '@/store'
    import routes from '@/router';  //引入路由文件
    
    ReactDOM.render(
      <Provider store={store}>   {/* redux */}
        <PersistGate loading={null} persistor={persistStore(store)}>   {/* 数据持久化 */}
          <BrowserRouter>   {/* 路由 */}
            {renderRoutes(routes)}
          </BrowserRouter>
        </PersistGate>
      </Provider>,
      document.getElementById('root')
    )
    

      

    第三步: router index.js 文件

    import { Home, Errors, Children01, Children02 } from './assembly';
    
    const routes = [
      { path: '/404', component: Errors },
      //嵌套路由
      { path: '/home', component: Home,
        routes: [
          { path: "/home/01", component: Children01 },
          { path: "/home/02", component: Children02 },
        ]
      },
    ]
    
    export default routes
    

      

    第四步: home页面渲染子路由

    import React, { Component } from 'react'
    import { renderRoutes } from 'react-router-config';
    import { Link } from "react-router-dom"; 
    
    export default class Home extends Component {
      render() {
        const { routes } = this.props.route
    
        return (
          <div>
            <Link to='/home/01'>页面1</Link>
            <Link to='/home/02'>页面2</Link>
            {renderRoutes(routes)} //渲染子路由
          </div>
        )
      }
    }
    

      

  • 相关阅读:
    洛谷P3128 [USACO15DEC]Max Flow P 题解 树上差分(点差分)
    数列分块解决区间更新+区间最值问题
    ThinkPad P1 Gen3 4K 显示器出现间歇闪黑屏情况解决
    Qt自定义弹出式菜单(Qt自定义弹窗)
    软件产品易用性评价评估标准
    vue用echarts实现中国地图和世界地图
    知了业务逻辑梳理
    string.gfind string.gmatch
    无法定位程序输入点在 XXXX上...
    [Lua]c解析lua 嵌套table
  • 原文地址:https://www.cnblogs.com/yetiezhu/p/12966026.html
Copyright © 2011-2022 走看看