如何实现react-router-dom嵌套路由
小编这次要给大家分享的是如何实现react-router-dom嵌套路由,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。
网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于企业网站制作,高端网页制作,对成都办公窗帘等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业seo优化优化,H5建站,响应式网站。
入口文件index.js:
import React from "react" import ReactDOM from "react-dom" import App from "./App.jsx" import "babel-polyfill" const root = document.getElementById("app") if (root !== null) { ReactDOM.render(, document.getElementById("app")) }
各个文件的详细位置:
主要目录App.jsx:
import React, { Fragment, Component } from "react" import { BrowserRouter as Router, Switch, Route, NavLink, Redirect, Link, withRouter, } from "react-router-dom" // 开始引入各种自定义的组件 import Index from "./pages/platform/index" //404页面 import ErrorPage from "./pages/ErrorPage" import Login from "./pages/login/index" import routes from "./routes/index" class BaseLayout extends Component { constructor(props) { super(props) } render(){ return() } } class App extends React.Component { constructor(props) { super(props) } render() { return ( {routes.map((val,index))=>{ return(
- {val.authName}
) }{routes.map((route, key) => { if (route.exact) { return ( { return ( ) } else { return () }} > {/* */} { return ( ) } })}) }} > {/* */} ) } } export default App
放路由route.js:
import Index from "../pages/platform/index" import UserAdd from "../pages/component/User/UserAdd" import UserList from "../pages/component/User/UserList" const routes = [ { path: "/index", authName: "首页", component: Index, exact: true, }, { path: "/user", authName: "用户1", component: UserList, }, { authName: "用户2", path: "/userAdd", component: UserAdd, }, ] export default routes
其他页面login.jsx:
import React, { Component } from "react" export default class Login extends Component { render() { returnLogin} }
其他页面index.jsx:
import React, { Component } from "react" export default class Index extends Component { render() { returnIndex} }
其他页面UserAdd.jsx:
import React, { Component } from "react" export default class UserAdd extends Component { render() { returnUserAdd} }
其他页面UserList.jsx:
import React, { Component } from "react" export default class UserList extends Component { render() { returnUserList} }
看完这篇关于如何实现react-router-dom嵌套路由的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。
新闻标题:如何实现react-router-dom嵌套路由
地址分享:http://azwzsj.com/article/gcjogs.html