当前位置: 代码迷 >> JavaScript >> 防止嵌套组件从以前的路径继承 (反应路由器4)
  详细解决方案

防止嵌套组件从以前的路径继承 (反应路由器4)

热度:59   发布时间:2023-06-03 18:10:40.0

我有一个 Dasboard 组件,其中我有 React-router <Link/>组件链接到

http:<procjethos>/team

<Team/>组件中,我有一个<Link/>组件应该链接到

http:<projecthost>/list/:listId

但相反,它继承自上一个链接,我得到

http<projecthost>/team/list/:listId

// 仪表板.jsx

  render() {
    const { shoppingLists, teams } = this.props;

    return (
      <Fragment>
        <Toolbar isHomePage/>
        <div className="wrapper">
          <div className="dashboard">
            <h2 className="dashboard__heading">
              <TeamIcon />
              Cohorts
            </h2>
            <ul className="dashboard__list">
              {_map(teams, team => (
                <li className="dashboard__list-item" key={team._id}>
                  <Link to={`team/${team._id}`}>
                    <CardItem name={team.name} />
                  </Link>
                </li>
              ))}
            </ul>
          </div>
        </div>
      </Fragment>
    );

// 团队.jsx

    <Fragment>
        <Toolbar />
        <div>
          Cohort of id:
          {id}
          <ul>
            {_map(lists, list => (
              <li className="dashboard__list-item" key={list._id}>
                <Link to={`list/${list._id}`} replace>
                  <CardItem name={list.name} />
                </Link>
              </li>
            ))}
          </ul>
        </div>
      </Fragment>

如何使用嵌套链接在 React-Router 4 中实现所需的行为?

在链接路径前添加/

 <Link to={`/list/${list._id}`} replace>
       <CardItem name={list.name} />
 </Link>
  相关解决方案