我想在我的项目中添加一个Bootstrap选项卡。但是,当从react-router-dom
而不是a
使用Link
时,它会导致问题。
<a href="#myID">DEMO</a>
<Link to="#myID">DEMO</Link>
在Chrome的控制台中,我发现在ID前面添加了一个"/“,这使得选项卡无法工作(我在控制台中通过”编辑属性“手动删除了"/”,它可以工作),我想知道我如何解决这个问题?
(在控制台中)
Link: href="/#myID"
a: href="#myID"
Bootstrap 4选项卡:https://www.w3schools.com/bootstrap4/bootstrap_ref_js_tab.asp
谢谢大家,
肯尼
发布于 2018-08-01 03:23:51
react-router
的Link
组件设计用于处理相对链接,因此他们必须插入这些斜杠才能组成有效的网址。
要解决此问题,您需要使用绝对路径:
更新使用路由器属性比使用全局变量更好:
// props from `react-router`'s Route
const Component = ({ location }) => (
// render stuff
<Link to={location.pathname + '#myID'}>DEMO</Link>
// render stuff
)
// assuming routes similar to
// ...
<Route path="somepath" component={Componet} />
// ...
使用全局变量document
的初始答案代码
<Link to={document.location.pathname + '#myID'} />
https://stackoverflow.com/questions/51620247
复制相似问题