reactjs 使用react-router-dom时弹出空白页面[重复]

xuo3flqw  于 2022-12-18  发布在  React
关注(0)|答案(1)|浏览(195)

此问题在此处已有答案

React Route render blank page(1个答案)
4小时前关门了。
我正在做一个预约应用程序。问题是当我点击“预约”,它应该带我到一个页面,在那里的形式是预约。我还没有作出的形式,因为我注意到没有弹出当我点击链接。(我应该看到一个h1标签包含文字说“bookingPage”)。只有两个路线。
App.jsx

  1. import { Route, Routes } from "react-router-dom"
  2. import BookingPage from "./pages/home/bookingPage/BookingPage"
  3. import Home from "./pages/home/Home"
  4. const App = () => {
  5. return (
  6. <Routes>
  7. { <Route index element={<Home />} /> }
  8. <Route path='/booking' component={BookingPage} />
  9. </Routes>
  10. )
  11. }
  12. export default App

第一条路径包含所有组件
Home.jsx

  1. import ContactSection from "../../Sections/Contact/ContactSection"
  2. import FAQsSection from "../../Sections/FAQs/FAQsSection"
  3. import Footer from "../../Components/Footer"
  4. import Header from "../../Components/Header"
  5. import Navbar from "../../Components/Navbar"
  6. import PolicySection from "../../Sections/Policy/PolicySection"
  7. import HeaderImage from '../../images/zamia.jpeg'
  8. import PricesSection from "../../Sections/Prices/PricesSection"
  9. const Home = () => {
  10. return (
  11. <>
  12. <Navbar />
  13. <Header title="Welcome Bossy Dolls !" image={HeaderImage}>
  14. Please read the following terms and contions before booking appointment.
  15. </Header>
  16. <PricesSection />
  17. <PolicySection />
  18. <ContactSection />
  19. <FAQsSection />
  20. <Footer />
  21. </>
  22. )
  23. }
  24. export default Home

第二条路线包含一个简单的
BookingPage.jsx

  1. import React from 'react'
  2. import './BookingPage.css'
  3. const BookingPage = () => {
  4. return (
  5. <h1 className='bookingHeader'>BookingPage</h1>
  6. )
  7. }
  8. export default BookingPage


我做了我通常做的事情,即 Package 周围的标签
index.js

  1. import ReactDOM from "react-dom/client";
  2. import App from './App'
  3. import './index.css'
  4. import { BrowserRouter } from "react-router-dom";
  5. const root = ReactDOM.createRoot(document.querySelector('#root'));
  6. root.render(
  7. <BrowserRouter>
  8. <App />
  9. </BrowserRouter>
  10. )

但是由于某种原因,当我点击一个链接,引导我到预订页面没有组件弹出。我试图删除App.jsx中的路线标签,但只会崩溃网站。我还试图添加路径&组件属性到App.jsx中的第一条路线,但也会崩溃网站。我不知道还能做什么。有什么建议吗?提前感谢。

up9lanfz

up9lanfz1#

好吧,你把两个版本的react router (v5 & V6)混在一起了。而且看起来你用的是react router v6
只需更改:

应用程序.jsx文件中的<Route path='/booking' component={BookingPage} />
收件人:

  1. <Route path="/booking" element={<BookingPage />} />

相关问题