如何在React上创建多个页面

发布于 2025-01-24 11:23:18 字数 687 浏览 3 评论 0原文

因此,我已经检查了多个帖子和网站,但我无法正常工作。它只是呈现带有Navbar的白色屏幕。 这是代码:

app.js:

import {BrowserRouter as Router, Routes, Route} from 'react-router-dom'


function App() {
  return (
    <Router>
    <Nav/>
    <Routes>
      <Route path='/about' element={About}/>
    </Routes>
    </Router>
  );
}

export default App;

index.js:

ReactDOM.render(<React.StrictMode><App/></React.StrictMode>, document.getElementById('root'))

某些组件:

<Link to="/about" className="btn btn-colored">Launch</Link>

“关于”组件正在工作原因,我已经测试并导入到app.js中,因此这不是问题。

So I've check multiple posts and websites and I can't get it to work. It just renders a white screen with a the navbar.
This is the code:

App.js:

import {BrowserRouter as Router, Routes, Route} from 'react-router-dom'


function App() {
  return (
    <Router>
    <Nav/>
    <Routes>
      <Route path='/about' element={About}/>
    </Routes>
    </Router>
  );
}

export default App;

Index.js:

ReactDOM.render(<React.StrictMode><App/></React.StrictMode>, document.getElementById('root'))

Some component:

<Link to="/about" className="btn btn-colored">Launch</Link>

The 'About' component is working cause I've tested and imported into App.js so that is not the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任谁 2025-01-31 11:23:18

您需要首先导入要渲染的组件,

例如您要呈现有关组件的内容,以便导入

  1. oil of the组件

大约导入“ ./ pages/about”

  1. 的;将语法固定为React-jsx中的语法,我们将其写入&lt;/&gt;括号。

&lt;路由路径=' /of of'element = {} /&gt;

You need to first import the component you want to render

For example you want to render About component so you would import the About component

  1. Import the component

import About from "./pages/About";

  1. Fix the syntax as in react-jsx we write it in </> brackets.

<Route path='/about' element={} />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文