小編這次要給大家分享的是如何實現(xiàn)react-router-dom嵌套路由,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站制作,高端網(wǎng)頁制作,對成都辦公窗簾等多個行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計,網(wǎng)站優(yōu)化推廣哪家好,專業(yè)seo優(yōu)化優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。
入口文件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} }
看完這篇關(guān)于如何實現(xiàn)react-router-dom嵌套路由的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。