简体   繁体   English

BrowserRouter,路由,路由器,路由不起作用

[英]BrowserRouter, Route, Router, Routes not working

I am learning to react and trying to build a nav bar.我正在学习做出反应并尝试建立一个导航栏。 But when I tried to use route to make my navbar links functional or add pages.但是当我尝试使用路由使我的导航栏链接功能或添加页面时。 It shows me a blank page.它向我显示了一个空白页。 But when I delete this portion <BrowserRouter>...</BrowserRouter> .但是当我删除这部分<BrowserRouter>...</BrowserRouter> My nav bar shows but as I don't use route there it won't show page components when I go to that page (ie http://localhost:3000/about).我的导航栏显示,但由于我不在那里使用路由,当我转到该页面时它不会显示页面组件(即 http://localhost:3000/about)。

Code:代码:

import React, { Component } from 'react';
import './App.css';
import 'whatwg-fetch';
import Series from '../../containers/Series';
import Navbar from '../Navbar';
import {BrowserRouter, Route, Router, Routes} from 'react-router-dom';
import About from '../../pages/about';
import Home from '../../pages/home';

function App() {
  return (
    <>
      <Navbar />
      <BrowserRouter>
        <Routes>
          <Route exact path="/" component={Home} />
          <Route exact path="/about" component={About} />
        </Routes>
      </BrowserRouter>
    </>
  );
}
 
export default App;

Did you read the docs of this library ?你读过这个图书馆的文档吗? BrouserRouter is used to keep your UI in sync with the URL. BrouserRouter用于使您的 UI 与 URL 保持同步。

You have to read guide section first of all.您必须首先阅读指南部分。 If you look carefully you see that you must use Switch component like a wrapper for your Route 's instead of Routes .如果你仔细看,你会发现你必须使用Switch组件,就像你的Route的包装器一样,而不是Routes

In your situation it must looks like在您的情况下,它必须看起来像

    <>
      <Navbar />
      <BrowserRouter>
        <Switch>
          <Route exact path="/" component={Home} />
          <Route exact path="/about" component={About} />
        </Switch>
      </BrowserRouter>
    </>

A Switch looks through its children Route 's and renders the first one that matches the current URL. Switch查看其子Route并呈现与当前 URL 匹配的第一个。

暂无
暂无

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

相关问题 为什么来自“react-router-dom”的 BrowserRouter 和 Route 无法正常工作? - Why is BrowserRouter and Route from 'react-router-dom' not working properly? 为什么BrowserRouter既是路由器又是路由器 - Why is BrowserRouter both a Router and a Route 我的 function App() 中的“BrowserRouter”、“Routes”和“Route”的结构有什么问题? - What is wrong with the structure of my 'BrowserRouter', 'Routes' and 'Route' in my function App()? 反应 BrowserRouter 导致“你不应该使用<Route>或 withRouter() 外<Router> ” - react BrowserRouter resulting in "You should not use <Route> or withRouter() outside a <Router>" react-router-dom BrowserRouter在构建后无法正常工作 - react-router-dom BrowserRouter not working after build BrowserRouter 不是一个<Route>零件。 的所有子组件<Routes>必须是<Route>或者<React.Fragment> - BrowserRouter is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment> 为什么我们需要将 BrowserRouter 包装在父组件中而不是路由子组件中以进行反应路由器 v6 - Why do we need to wrap the BrowserRouter in parent component instead of routes child component for react router v6 反应路由器:路由不起作用 - React router: Route not working 在路由内反应路由器嵌套路由 - React router nesting routes inside route 当在BrowserRouter组件中更新道具时,未调用react-router Route组件构造函数 - react-router Route component constructor is not called when props are updated in BrowserRouter component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM