反应路由:URL正在更改,但页面未加载

时间:2020-03-30 04:29:23

标签: javascript reactjs react-router

在React(v16.13.1)中, URL发生更改,但页面未加载

我在堆栈中发现了类似的问题ReactJS - React Router not changing component but url is changing React Router URL Change but Not Component,我尝试了不同的方法,但没有一个帮助我。

可能是一个小错误,请帮助纠正。

我的代码如下:

App.js文件

import React, { Component } from "react";
import { BrowserRouter as Router, Route,Switch,Redirect } from "react-router-dom";
import TopNav from "./components/Navbar/TopNav";
import SideNav from "./components/Navbar/SideNav";
import Dashboard from "./components/Dashboard";
import Analytics from "./components/Analytics";
import ToDo from "./components/ToDo";

class App extends React.Component {
  render() {
    return (
      <div>
        <TopNav />
        <SideNav />
        <Router>
          <Switch>
            <Route exact path="/">
              <Dashboard />
            </Route>
            <Route exact path="/analytics">
              <Analytics />
            </Route>
            {/* <Route exact path="/analytics" component={Analytics}></Route> */}
            <Route exact path="/todo">
              <ToDo />
            </Route>
            <Redirect to="/"></Redirect>
          </Switch>
        </Router>
      </div>
    );
  }
}
export default App;

SideNav.js页面如下:

import React from "react";
import { BrowserRouter as Router, Link } from "react-router-dom";

class SideNav extends React.Component {
  render() {
    return (
      <div className="sidebar-wrapper">
        <div className="sidebar-container">
          <Router>
            <ul className="sidebar-menu">
              <li className="active">
                <i className="fa fa-cubes"></i>
                <Link className="link" to="/">
                  Dashboard
                </Link>
              </li>
              <li>
                <i className="fa fa-pie-chart"></i>
                <Link className="link" to="/analytics">
                  Analytics
                </Link>
              </li>
              <li>
                <i className="fa fa-medkit"></i>
                <Link className="link" to="/todo">
                  What to do?
                </Link>
              </li>
            </ul>
          </Router>
        </div>
      </div>
    );
  }
}
export default SideNav;

谢谢!

2 个答案:

答案 0 :(得分:1)

您只需要在应用程序的顶层安装一个<Router>

您已经在<Router>组件中使用App

SideNav中包装Router组件,并删除Router中使用的SideNav

import React, { Component } from "react";
import {
  BrowserRouter as Router,
  Route,
  Switch,
  Redirect,
  Link
} from "react-router-dom";

class SideNav extends Component {
  render() {
    return (
      <div className="sidebar-wrapper">
        <div className="sidebar-container">
          {/* <Router> */}
          <ul className="sidebar-menu">
            <li className="active">
              <i className="fa fa-cubes" />
              <Link className="link" to="/">
                Dashboard
              </Link>
            </li>
            <li>
              <i className="fa fa-pie-chart" />
              <Link className="link" to="/analytics">
                Analytics
              </Link>
            </li>
            <li>
              <i className="fa fa-medkit" />
              <Link className="link" to="/todo">
                What to do?
              </Link>
            </li>
          </ul>
          {/* </Router> */}
        </div>
      </div>
    );
  }
}

class App extends Component {
  render() {
    return (
      <div>
        <Router>
          <SideNav />
          <Switch>
            <Route exact path="/">
              <div>/</div>
            </Route>
            <Route exact path="/analytics">
              <div>ana</div>
            </Route>
            <Route exact path="/todo">
              <div>todo</div>
            </Route>
            <Redirect to="/" />
          </Switch>
        </Router>
      </div>
    );
  }
}
export default App;

答案 1 :(得分:1)

您的def display(pic): for y in range(500): for x in range(500): screen.set_at((x,y),pic[y][x]) 和主要内容位于单独的<SideNav />中。他们没有看到对方,也无法一起工作。
想想,就像两个独立的iframe彼此独立导航一样,而没有实际的iframe。

所有需要重新配置路由器的内容,基本上是整个应用,都需要包含在单个<Router />中。

<Router />

SideNav.js

import React, { Component } from "react";
import { BrowserRouter as Router, Route, Switch, Redirect } from "react-router-dom";
import TopNav from "./components/Navbar/TopNav";
import SideNav from "./components/Navbar/SideNav";
import Dashboard from "./components/Dashboard";
import Analytics from "./components/Analytics";
import ToDo from "./components/ToDo";

class App extends React.Component {
  render() {
    return (
      <Router>
        <TopNav />
        <SideNav />
        <Switch>
          <Route exact path="/">
            <Dashboard />
          </Route>
          <Route exact path="/analytics">
            <Analytics />
          </Route>
          {/* <Route exact path="/analytics" component={Analytics}></Route> */}
          <Route exact path="/todo">
            <ToDo />
          </Route>
          <Redirect to="/"></Redirect>
        </Switch>
      </Router>
    );
  }
}
export default App;

import React from "react"; import { BrowserRouter as Router, Link } from "react-router-dom"; class SideNav extends React.Component { render() { return ( <div className="sidebar-wrapper"> <div className="sidebar-container"> <ul className="sidebar-menu"> <li className="active"> <i className="fa fa-cubes"></i> <Link className="link" to="/"> Dashboard </Link> </li> <li> <i className="fa fa-pie-chart"></i> <Link className="link" to="/analytics"> Analytics </Link> </li> <li> <i className="fa fa-medkit"></i> <Link className="link" to="/todo"> What to do? </Link> </li> </ul> </div> </div> ); } } 也是如此。