简体   繁体   English

在React Router的最新版本中,如何在首页“ /”中嵌套路由

[英]In React router's latest version, how to nest routes in the homepage “/”

How to nest routes in the homepage "/" ? 如何在首页“ /”中嵌套路线?

let's say you have a 2 page site, the HOME page with just a header and paragraph and the ABOUT page have 2 more nested routes, here's the about page. 假设您有一个2页的网站,HOME页仅包含标题和段落,而ABOUT页则有2条嵌套的路线,这是About页。

ABOUT 关于

import React from "react";
import { Link, Route, Switch, Redirect } from "react-router-dom";
/* Inner Pages */
import Technology from "./innerpages/Technology";
import Business from "./innerpages/Business";


const About = props => {
  return (
    <div className="container">
        <div className="tab-navs">
            <Link to={`${props.match.url}/business`}>Business</Link>
            <Link to={`${props.match.url}/economics`}>Economics</Link>
        </div>
          <Switch>
            <Route
              exact
              path={`${props.match.path}/business`}
              render={() => <Business />}
            />
            <Route
              path={`${props.match.path}/economics`}
              render={() => <Economics />}
            />
          </Switch>
    </div>
  );
};

export default About;

HOME

import React from "react";

const Home = props => {
  return (
    <div className="container">
        <h1>Home</h1>
        <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit.</p>
    </div>
  );
};

export default Home;

Here's the index.js 这是index.js

INDEX 指数

import React,{Component} from "react";
import ReactDOM from "react-dom";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import Logo from "./components/Logo";
import Navigation from "./components/Navigation";

/* Pages */
import Home from "./pages/Home";
import About from "./pages/About";

import "./stylesheet/main.scss";

class App extends Component {
  constructor(props){
     super(props);
  }
 render(){
  return (
    <Router>
      <div className="App">
        <header>
          <Logo />
          <Navigation />
        </header>
        <main>
          <Switch>
            <Route path="/" exact render={props => <Home />} />
            <Route path="/about" component={About} />
          </Switch>
        </main>
      </div>
    </Router>
  );
}
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Now this works fine, but what if the HOMEPAGE with a url of "/" has the 2 nested routes? 现在可以正常工作,但是如果URL为“ /”的HOMEPAGE具有2个嵌套路由,该怎么办? i tried switching their content and it doesn't work 我试图切换他们的内容,但没有用

If Homepage has to have nested Routes, what you would do is to remove the exact keyword from its path and reorder your Routes within Switch component 如果主页必须具有嵌套的路由,则要做的是从其路径中删除完全相同的关键字,然后在Switch组件中对路由进行重新排序

  <Switch>
        <Route path="/about" component={About} />
        <Route path="/services" component={Services} />
        <Route path="/" render={props => <Home {...props}/>} />
  </Switch>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM