Javascript React链接添加不需要的/(React JS)

Javascript React链接添加不需要的/(React JS),javascript,reactjs,Javascript,Reactjs,我想在我的项目中添加一个引导选项卡。但是当使用react router dom中的Link而不是a时,会导致问题 <a href="#myID">DEMO</a> <Link to="#myID">DEMO</Link> 引导4选项卡: 谢谢大家 肯尼react路由器的链接组件设计用于处理相对链接,因此他们必须插入这些斜杠才能组成有效的URL 要解决此问题,您需要使用绝对路径: UPD使用路由器道具比使用全局变量更好: // props fro

我想在我的项目中添加一个引导选项卡。但是当使用
react router dom
中的
Link
而不是
a
时,会导致问题

<a href="#myID">DEMO</a>
<Link to="#myID">DEMO</Link>
引导4选项卡:

谢谢大家


肯尼

react路由器
链接
组件设计用于处理相对链接,因此他们必须插入这些斜杠才能组成有效的URL

要解决此问题,您需要使用绝对路径:

UPD使用路由器道具比使用全局变量更好:

// 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} />
// ...
//来自'react router'路由的道具
常量组件=({location})=>(
//渲染材料
演示
//渲染材料
)
//假设路线与
// ...
// ...

使用全局变量的初始答案代码
文档


请查看react引导程序项目链接组件从何处导入?@amankkg来自react路由器dom
// 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} />
// ...