这篇文章主要介绍了模块化react-router如何配置,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创新互联公司服务项目包括大祥网站建设、大祥网站制作、大祥网页制作以及大祥网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,大祥网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到大祥省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
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 routes4.在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.map((router,index)=>{
if(router.exact){
return (
)
}
/>
}else{
return (
)
}
/>
}
})
}
);
}
export default App;注意点:嵌套路由千万不要在
注意点:嵌套路由千万不要在
注意点:嵌套路由千万不要在
解析一下,
5.在有子路由的页码配置跳转
import React ,{Component} from 'react';
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
class New extends Component{
render(){
return(
-
New
-
NewList
{
this.props.routes.map((item,index)=>{
return
})
}
)
}
}
export default New最后的结果为:

感谢你能够认真阅读完这篇文章,希望小编分享的“模块化react-router如何配置”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
新闻名称:模块化react-router如何配置
当前链接:http://www.jxjierui.cn/article/jpsigc.html


咨询
建站咨询
