为什么 React-Router 不显示链接?

我第一次尝试React-Router,并尝试编写一些代码,但链接似乎不起作用。这些是我的链接。起初,当我将链接放入应用程序组件中时,链接起作用了。然后我尝试将导航放入它自己的组件中,然后链接不起作用。感谢您的帮助!


import React from "react";

import About from "./About";

import Home from "./Home";

import Navigation from "./Navigation";

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


export default function App() {

  return (

    <Router>

      <div>

        <Navigation />

        <Switch>

          <Route path="/about">

            <About />

          </Route>

          <Route path="/" exact>

            <Home />

          </Route>

        </Switch>

      </div>

    </Router>

  );

}


import React from "react";

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


export default function Navigation() {

  return (

    <nav>

      <ul>

        <li>

          <Link to="/">Home</Link>

        </li>

        <li>

          <Link to="/about">About</Link>

        </li>

      </ul>

    </nav>

  );

}


潇湘沐
浏览 89回答 1
1回答

慕容3067478

您正在导入import { BrowserRouter as Link } from "react-router-dom";它应该是import { Link } from "react-router-dom";
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript