我有这种Router
,我发现的副作用是,当我例如在一些localhost:3000/tododetails/some_id
中,我点击链接例如:Notes Form
,它不会引导我到localhost:3000/notesform
,但它确实直接到localhost:3000/tododetails/notesform
,这显然是没有定义的。我如何能够编辑它正确地工作?试图在Link
内部创建一个绝对路径,像这样:localhost:3000/notesform
,但结果是localhost: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>
必须以/
开头,如下所示:
<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>
否则视为相对链接。