javascript webpack编译成功,但未显示结果

balp4ylt  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(138)

我已经导入了所有的路由器,但是当我去localhost服务器网站的时候,我看到的只是一个空白页面。我已经设置了主页和其他的。

import React from "react";
import './App.css';

import Home from "./pages/Home";
import Rooms from "./pages/Rooms";
import SingleRoom from "./pages/SingleRoom";
import Error from "./pages/Error";

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

import Navbar from "./components/Navbar"

function App() {
  return (
    <>
    <Navbar />
    <Switch>
      <Route exact path="/" component={<Home/>} />
      <Route exact path="/rooms/" component={<Rooms/>} />
      <Route exact path="/rooms/:slug" component={<SingleRoom/>} />
      <Route component={<Error/>} />
    </Switch>
    

    
    </>
  );
}

export default App;

下面是index.js文件,我在这里导入浏览器路由器。

import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter as Router } from 'react-router-dom';
import App from './App';

ReactDOM.render(
  <Router>
    <App />
  </Router>,
  document.getElementById("root")
);

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

我检查了路由器的导入,看不出哪里有问题。

pqwbnv8z

pqwbnv8z1#

下面是针对您的问题更新的index.js文件。

import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter as Router } from 'react-router-dom';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
   <Router>
    <App />
  </Router>
  </React.StrictMode>
);

相关问题