首页 > 解决方案 > 如何建立直接链接,从而重置 React-router-dom 中的整个路径

问题描述

我有这种情况,我发现的Router副作用是,例如,当我在某些地方并localhost:3000/tododetails/some_id单击例如链接:我怎样才能编辑它以正常工作?试图在里面创建一个绝对路径,像这样:,但结果比以前更糟。Notes Formlocalhost:3000/notesformlocalhost:3000/tododetails/notesformLinklocalhost:3000/notesformlocalhost:3000/tododetails/localhost:3000/notesform

我的路由器:

<BrowserRouter>
          <ul>
              <li><Link to="todolist">Todo List</Link></li>
              <li><Link to="todoform">Todo Form</Link></li>
              <li><Link to="noteslist">Notes List</Link></li>
              <li><Link to="notesform">Notes Form</Link></li>
          </ul>

          <Route exact path="/todolist" component={TodoList} />
          <Route exact path="/todoform" component={TodoForm} />
          <Route exact path="/todoform/:id" component={TodoForm} />
          <Route exact path="/tododetails/:id" component={TodoDetails} />

          <Route exact path="/noteslist" component={NotesList} />
          <Route exact path="/notesform" component={NotesForm} />
          <Route exact path="/notesform/:id" component={NotesForm} />
          <Route exact path="/notesdetails/:id" component={NotesDetails} />
      </BrowserRouter>

标签: javascriptreactjsreact-router-dom

解决方案


您必须以 a 开头的链接值/如下:

<li><Link to="/todolist">Todo List</Link></li>
<li><Link to="/todoform">Todo Form</Link></li>
<li><Link to="/noteslist">Notes List</Link></li>
<li><Link to="/notesform">Notes Form</Link></li>

否则,它被认为是一个相对链接。


推荐阅读