【问题标题】:React link is adding characters to my urlReact 链接正在向我的网址添加字符
【发布时间】:2021-12-31 17:01:41
【问题描述】:

我想用我的 Link 组件(来自 react-router-dom)重定向到一个完全不同的 url,但它只是将这些字符添加到我当前的 url。 例如,如果我单击“http://localhost:3000/pin-detail/5HHceZ3LuAltt4AEKn2LYc”上的此链接

<Link to={`user-profile/${pinDetail.postedBy?._id}`} className='flex gap-2 m5-5 items-center bg-white dark:bg-darkg rounded-lg mt-2'>
                    <img src={pinDetail.postedBy?.image} alt="user-profile" className='w-8 h-8 rounded-full object-cover' />
                    <p className='font-semibold capitalize dark:text-white'>{pinDetail.postedBy?.userName}</p>
 </Link>

它转到“http://localhost:3000/pin-detail/5HHceZ3LuAltt4AEKn2LYc/user-profile/104293279805278412025”。我希望链接将我重定向到“localhost:3000/user-profile/${pinDetail.postedBy?._id”。 我该怎么做?

【问题讨论】:

  • 您使用的是亲戚to。以/ 为前缀,例如to={'/user-profile...'}
  • 哦,谢谢。成功了!

标签: reactjs react-router


【解决方案1】:

您需要在马特评论之前添加一个 / ,就像这样

<Link to={`/user-profile/${pinDetail.postedBy?._id}`} className='flex gap-2 m5-5 items-center bg-white dark:bg-darkg rounded-lg mt-2'>
                    <img src={pinDetail.postedBy?.image} alt="user-profile" className='w-8 h-8 rounded-full object-cover' />
                    <p className='font-semibold capitalize dark:text-white'>{pinDetail.postedBy?.userName}</p>
 </Link>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    相关资源
    最近更新 更多