简体   繁体   English

为什么不根据路由渲染组件?

[英]Why aren't components being rendered according to route?

I'm using react router and I have created a route with the path /account which renders the Account component.我正在使用反应路由器,并且我创建了一个带有路径/account的路由,它呈现了 Account 组件。 That component renders a navbar.该组件呈现一个导航栏。 Below that navbar I want it to render a different component depending on what the URL is.在该导航栏下方,我希望它根据 URL 的内容呈现不同的组件。 If the URL is account/edit I want to show the edit account component, if the URL is account/myorders I want it to show my orders component and lastly if the URL is account/favorites I want it to show the favorites component below my navbar, If the URL is account/edit I want to show the edit account component, if the URL is account/myorders I want it to show my orders component and lastly if the URL is account/favorites I want it to show the favorites component below my导航栏,

  • Edit Account编辑帐户
  • My Orders我的订单
  • Favorites收藏夹

Now I have this problem that the url changes but no component renders below my navbar.现在我遇到了这个问题,url 发生了变化,但在我的导航栏下方没有呈现任何组件。 if I use exact path on the /account route I get " path does not exist " on the routes /edit , /myorders and /favorites .如果我在/account路线上使用确切的路径,我会在/edit/myorders/favorites路线上得到“路径不存在”。 if I don't use exact on the /account route the same view shows on all routes.如果我不在/account路由上使用exact,则所有路由上都会显示相同的视图。 Only time when I get a component to render is if I change the route on for example /edit to / .只有当我得到一个组件来渲染时,如果我将例如/edit/的路线。

function Routes() {
  return (
    <Switch>
      <Route path="/" component={Home} exact />

      <Route path="/account" component={Account} />

      <Route render={() => <Route component={Error} />} />
    </Switch>
  );
}

These are my already existing routes that works that are imported into my App.js component这些是导入到我的 App.js 组件中的我已经存在的有效路线

const Account = () => {
  return (
    <Router>
      <NavBar />
      <Switch>
        <Route path="/edit" component={EditAccount} exact />
        <Route path="/myorders" component={MyOrders} />
        <Route path="/favorites" component={Favorites} />
      </Switch>
    </Router>
  );
};

These are the routes in my Account.js component that does not work这些是我的 Account.js 组件中不起作用的路由

The problem lies with the fact that you are using <Router></Router> to wrap your routes inside the Acouunt component.问题在于您使用<Router></Router>routes包装在Acouunt组件中。

Try to remove that, something like this:-尝试删除它,如下所示:-

const Account = () => {
  return (
      <NavBar />
      <Switch>
        <Route path="/edit" component={EditAccount} exact />
        <Route path="/myorders" component={MyOrders} />
        <Route path="/favorites" component={Favorites} />
      </Switch>
  );
};

Check this answer for more info. 检查此答案以获取更多信息。

The soloution for me was to use nested routes like this.我的解决方案是使用这样的嵌套路由。

const Account = () => {
  let match = useRouteMatch();

  return (
    <Router>
      <NavBar />
      <h1>Account</h1>
      <Switch>
        <Route path={`${match.path}/edit`} component={EditAccount} exact />
        <Route path={`${match.path}/myorders`} component={MyOrders} />
        <Route path={`${match.path}/favorites`} component={Favorites} />
      </Switch>
    </Router>
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM