React路由不起作用React路由器DOM

huangapple go评论69阅读模式
英文:

React Routes is not working React Router dom

问题

路由不起作用,我是React的初学者。当我在浏览器中输入URL时,文本不显示。我尝试了以下方法。我在pages文件夹中创建了页面。

import Index from "./pages/Index";
import Login from "./pages/Login";
import Product from "./pages/Product";
import Register from "./pages/Register";
import { BrowserRouter, Route } from 'react-router-dom';

function App() {
  return (
    <div>
      <BrowserRouter>
        <Route path="/" component={Index} />
        <Route path="/login" component={Login} />
        <Route path="/product" component={Product} />
        <Route path="/register" component={Register} />
      </BrowserRouter>
    </div>
  );
}

export default App;

请确保您已正确导入所需的组件和库,并且已安装了react-router-dom库。如果问题仍然存在,请检查浏览器控制台是否有任何错误消息,并确保您的路由路径与实际页面组件的路径匹配。

英文:

Route is not working i am a beginner of react.when i type the urls on the browser text is not displaying.what i tried so far i attached the below. i created the pages inside pages folder.

import Index from &quot;./pages/Index&quot;;
import Login from &quot;./pages/Login&quot;;
import Product from &quot;./pages/Product&quot;;
import Register from &quot;./pages/Register&quot;;
import {BrowserRouter,Route} from &#39;react-router-dom&#39;;

function App() {
  return (
   &lt;div&gt;
      &lt;BrowserRouter&gt;
         
          &lt;Route path = &quot;/&quot;&gt;
            &lt;Index/&gt;
          &lt;/Route&gt;

          &lt;Route path = &quot;/login&quot;&gt;
            &lt;Login/&gt;
          &lt;/Route&gt;

          &lt;Route path = &quot;/product&quot;&gt;
            &lt;Product/&gt;
          &lt;/Route&gt;

          &lt;Route path = &quot;/register&quot;&gt;
            &lt;Register/&gt;
          &lt;/Route&gt;
      
      &lt;/BrowserRouter&gt;

   &lt;/div&gt; 
    
  );
}

export default App;

答案1

得分: 2

你需要将所有的路由包裹在 Routes 组件内。这是 react-router-dom 的一部分。

import Index from "./pages/Index";
import Login from "./pages/Login";
import Product from "./pages/Product";
import Register from "./pages/Register";
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <div>
      <BrowserRouter>
        <Routes>
          <Route path="/" element={<Index />} />
          <Route path="/login" element={<Login />} />
          <Route path="/product" element={<Product />} />
          <Route path="/register" element={<Register />} />
        </Routes>
      </BrowserRouter>
    </div>
  )
}

export default App;
英文:

You need to wrap all your routes inside Routes component. It's part of the react-router-dom

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

import Index from &quot;./pages/Index&quot;;
import Login from &quot;./pages/Login&quot;;
import Product from &quot;./pages/Product&quot;;
import Register from &quot;./pages/Register&quot;;
import {BrowserRouter,Routes,Route} from &#39;react-router-dom&#39;;

function App() {
  return (
    &lt;div&gt;
      &lt;BrowserRouter&gt;
        &lt;Routes&gt;
          &lt;Route path = &quot;/&quot; element={&lt;Index /&gt;} /&gt;
          &lt;Route path = &quot;/login&quot; element={&lt;Login /&gt;} /&gt;
          &lt;Route path = &quot;/product&quot; element={&lt;Product /&gt;} /&gt;
          &lt;Route path = &quot;/register&quot; element={&lt;Register/&gt;} /&gt;
        &lt;/Routes&gt;
       &lt;/BrowserRouter&gt;
     &lt;/div&gt; 
 )}

export default App;

<!-- end snippet -->

答案2

得分: 0

将所有路由包装在像这样的routes中:

function App() {

  return (
    <HashRouter>
      <Routes>
        <Route path="/" element={<Index />} />
        <Route path="/login" element={<Login />} />
      </Routes>
    </HashRouter>
  )
}
英文:

Wrap all route in routes like this

function App() {

  return (
    &lt;HashRouter&gt;
      &lt;Routes&gt;
        &lt;Route path=&quot;/&quot; element={&lt;Index/&gt;} /&gt;
        &lt;Route path=&quot;/login&quot; element={&lt;Login/&gt;} /&gt;
      &lt;/Routes&gt;
    &lt;/HashRouter&gt;
  )
}

答案3

得分: 0

每个路由都使用 element 属性在其 props 中显示你指定的元素。

尝试这样做:

import Index from "./pages/Index";
import Login from "./pages/Login";
import Product from "./pages/Product";
import Register from "./pages/Register";
import { BrowserRouter, Route } from 'react-router-dom';

function App() {
  return (
    <div>
      <BrowserRouter>
        <Route path='/'>
          <Route index element={<Index />} />
          <Route path="login" element={<Login />} />
          <Route path="product" element={<Product />} />
          <Route path="register" element={<Register />} />
        </Route>
      </BrowserRouter>
    </div>
  );
}

export default App;

额外信息:

这样做的效果是你在路径 / 上指定了一个主页路由,所有其他路由都是这个主页路由的子路由。

在第一个嵌套路由中的 index 属性用于告诉程序这是父级的首页页面。

英文:

Each Route shows the element you specify in its props using the element prop.

Try this:

import Index from &quot;./pages/Index&quot;;
import Login from &quot;./pages/Login&quot;;
import Product from &quot;./pages/Product&quot;;
import Register from &quot;./pages/Register&quot;;
import {BrowserRouter,Route} from &#39;react-router-dom&#39;;

function App() {
  return (
   &lt;div&gt;
      &lt;BrowserRouter&gt;
          &lt;Route path=&#39;/&#39;&gt;
            &lt;Route index element={&lt;Index/&gt;} /&gt;
            &lt;Route path=&quot;login&quot; element={&lt;Login/&gt;} /&gt;
            &lt;Route path=&quot;product&quot; element={&lt;Product/&gt;} /&gt;
            &lt;Route path=&quot;register&quot; element={&lt;Register/&gt;} /&gt;
          &lt;/Route&gt;
      &lt;/BrowserRouter&gt;
   &lt;/div&gt;
  );
}

export default App;

Extra context:

What this does is that you specify a home route at the path / and all other routes are children of this home route.

The index property in the first nested route is used to tell the program this is the index page of the parent.

答案4

得分: 0

你需要在Routes标签中传递element属性,指定路径应该导向的元素。你可以在这里找到文档。

这应该可以工作:

<Route index element={<Index/>}/>
<Route path="login" element={<Login/>}/>
<Route path="product" element={<Product/>}/>
<Route path="register" element={<Register/>}/>
英文:

You would need to pass the element prop where the path should direct to in the Routes tag. You can find documentation here.

This should work:

&lt;Route index element={&lt;Index/&gt;}/&gt;
&lt;Route path=&quot;login&quot; element={&lt;Login/&gt;}/&gt;
&lt;Route path=&quot;product&quot; element={&lt;Product/&gt;}/&gt;
&lt;Route path=&quot;register&quot; element={&lt;Register/&gt;}/&gt;

huangapple
  • 本文由 发表于 2023年1月9日 15:54:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定