我想使用我的 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”。我怎樣才能做到這一點?
uj5u.com熱心網友回復:
您需要像馬特評論中所說的那樣在之前添加 / ,就像這樣
<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399856.html
下一篇:過濾反應組件狀態導致無限渲染回圈
