模块化react-router配置

栏目: IOS · Android · 发布时间: 4年前

内容简介:因为公司的需要最近踏进了react坑,一直在挖坑填坑,在路由这一块折腾得不行。直接进入主题,配置react-router模块化npm install react-router-dom --save

react-router模块化配置

因为公司的需要最近踏进了react坑,一直在挖坑填坑,在路由这一块折腾得不行。

直接进入主题,配置react-router模块化

  • 1.先下载react-router-dom

npm install react-router-dom --save

  • 2.在相应的文件引入react-router-dom相应的模块
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
  • 3.在src子创建一个module目录,接着在module目录在创建一个router.js文件,用来配置路由。
//router.js
import Index from '../components/Index'
import New from '../components/New'
    import NewList from '../components/NewList'
    import NewContent from '../components/NewContent'
    
const routes = [
    
    {
        path:"/",
        component:Index,
        exact:true
    },
    {
        path:"/new",
        component:New,
        routes:[
            {
                path:"/new/",
                component:NewContent
            },
            {
                path:"/new/newList",
                component:NewList
            }
        ]
    },
    
]

export default routes
  • 4.在app.js根目录添加相应的跳转路径。
//app.js

import React from 'react';
import './App.css';
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import router from "./modules/routers"

function App() {
  return (
    <Router>
            <nav className="nav">
                <ul>
                    <li>
                        <Link to="/">首页</Link>
                    </li>
                    <li>
                        <Link to="/new">新闻</Link>
                    </li>
                </ul>
            </nav>
            {
                router.map((router,index)=>{
                    
                        if(router.exact){
                            
                            return <Route exact key={index} path={router.path}
                                render = {
                                    props =>(
                                        <router.component {...props} routes = {router.routes}/>
                                    )
                                }
                            />
                            
                        }else{
                            
                            return <Route key={index} path={router.path}
                                render = {
                                    props =>(
                                        <router.component {...props} routes = {router.routes} />
                                    )
                                }
                            />
                            
                        }
                    
                })
            }
        </Router>
  );
}

export default App;

注意点:嵌套路由千万不要在<Route/>身上加上component={xxx.xxx},否则在子路由页码就接受不到父路由传递给子路由的数据,重要的事情说三篇

注意点:嵌套路由千万不要在<Route/>身上加上component={xxx.xxx},否则在子路由页码就接受不到父路由传递给子路由的数据,重要的事情说三篇

注意点:嵌套路由千万不要在<Route/>身上加上component={xxx.xxx},否则在子路由页码就接受不到父路由传递给子路由的数据,重要的事情说三篇

解析一下,<Route/>里面的render,这是官方给出的一种固定写法,为了解决父路由传递数据给子路由接受不到的问题。render是一个对象,里面是一个箭头函数,箭头函数放回<router.component {...props} routes = {router.routes} />一个标签,router.component的router对于的是你map传进来的那个形参,传啥写啥;component 是配置文件对应的component ,routes 是传给子路由的数据、(子路由通过this.props.routes 接收)

  • 5.在有子路由的页码配置跳转
import React ,{Component} from 'react';

import { BrowserRouter as Router, Route, Link } from "react-router-dom";

class New extends Component{
    
    render(){
        
        return(
            
            <div className="box">
                <div className="left">
                    <ul>
                        <li>
                            <Link to="/new">New</Link>
                        </li>
                        <li>
                            <Link to="/new/newList">NewList</Link>
                        </li>
                    </ul>
                </div>
                <div className="right">
                    {
                        this.props.routes.map((item,index)=>{
                            
                            return <Route key={index} exact path={item.path} component={item.component}></Route>
                            
                        })
                    }
                </div>
            </div>
            
        )
        
    }
    
}

export default New

最后的结果为:

模块化react-router配置


以上所述就是小编给大家介绍的《模块化react-router配置》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Google御用網頁語言Node.js

Google御用網頁語言Node.js

郭家寶 / 佳魁資訊 / 2013-4-26 / NT 490

這是一本 Node.js 的入門教學,寫給想要學習 Node.js,但沒有任何系統的經驗的開發者。如果你聽說過 Node.js,並被它許多神奇的特性吸引,本書就是為你準備的。 透過閱讀本書,你可以對 Node.js 有全面的認識,學會如何用 Node.js 程式設計,了解事件驅動、非同步式 I/O 的程式設計模式,同時還可以了解一些使用JavaScript 進行函數式程式設計的方法。 ......一起来看看 《Google御用網頁語言Node.js》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具