reactjs - React Router v6 不会使用浏览器 URL 渲染/同步特定路由
问题描述
我有一些这样配置的路由:
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</Router>
问题是当我打开时/
,转到/about
然后转到/contact
;URL 变为/about/contact
and not /contact
,除了页面未呈现。如何修复它,我使用的是6.0.0-Alpha-3版本。我已阅读此版本的文档,并且知道斜杠/contact
表示绝对路径而不是相对路径contact
。
链接:
import { Link } from 'react-router-dom';
const ListItemLink = (props) => {
// React Router link
return <ListItem button component={Link} {...props} />;
};
// Material UI components
<ListItemLink href="/about">
<ListItemText primary="Home" />
</ListItemLink>
<ListItemLink href="/contact">
<ListItemText primary="Home" />
</ListItemLink>
解决方案
您必须使用 to 而不是 href 道具。
推荐阅读
- asp.net-core-mvc - User.Identity.IsAuthenticated = false 登录成功后使用 web api mvc core 3.0
- reactjs - ReactJS:从另一个子组件调用子方法?
- oracle - 创建表失败
- php - 如何计算5星总评分的平均值?
- winforms - VS2017/2017 中缺少 InvokeRequired 属性
- python-3.x - 无法在 Selenium 中使用 Python 进一步测试搜索
- angularjs - 文件上传 AngularJS 和 Django:/uploaded_file uploadFile() 处的 TypeError 缺少 1 个必需的位置参数:“请求”
- r - R optim,我可以将列表传递给参数:par 吗?
- security - 如何为我们的网络实施 RPKI?
- hibernate - 如何使用 Hibernate 中的 Criteria 从 3-4 个数据库表中获取数据。?