英文:
Logo isn't loading on nested routes
问题
以下是您要翻译的内容:
我正在使用react-router-dom版本6.11.1。在我的页眉组件中,标志是一个图像标签,包含在我的public文件夹中的图像。在我的单一路由组件中,我可以看到标志,但当它进入一个子路由时,我不能再看到标志,它不再加载了。
在我的情况下,它在reset_password路由中。
我也测试了路由,结果是一样的。有人可以帮助我吗?
index.js文件
const router = createBrowserRouter(
createRoutesFromElements(
<Route path='/' element={<App />}>
<Route index={true} path='/' element={<Homepage />} />
<Route path='/login' element={<Login />} />
<Route path='/signup' element={<Signup />} />
<Route path='/verification' element={<VerifyUser />} />
<Route path='/forgot-password' element={<ForgotPassword />} />
<Route path='/dashboard' element={<Dashboard />} />
<Route path='/reset_password/:id' element={<ResetPassword />} />
</Route>
)
);
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<Provider store={store}>
<RouterProvider router={router} />
</Provider>
</React.StrictMode>
);
和页眉组件
<header>
<nav className='navbar navbar-expand-lg'>
<div className='container'>
<Link to='/' className='navbar-brand'>
<img
className='logo'
loading='lazy'
src='./images/jk_logo_horizontal.png'
alt='logo'
/>
</Link>
<div>
<ul className='navbar-nav ms-auto mb-2 mb-lg-0'>
<li className='nav-item'>
<Link to='/login' className='nav-link active' aria-current='page'>
<button className='btn btn-login'>Login</button>
</Link>
</li>
</ul>
</div>
</div>
</nav>
</header>
英文:
I am using react-router-dom version 6.11.1. In my header component the logo is a image tag the included image which is in my public folder. In my single route component i can see the logo but when it goes 1 step down route like nested route i can't the see the logo its not loading anymore.
In my case it is in reset_password route.
I tested with routes as well its is the same. Can anyone help me with this
index.js file
const router = createBrowserRouter(
createRoutesFromElements(
<Route path='/' element={<App />}>
<Route index={true} path='/' element={<Homepage />} />
<Route path='/login' element={<Login />} />
<Route path='/signup' element={<Signup />} />
<Route path='/verification' element={<VerifyUser />} />
<Route path='/forgot-password' element={<ForgotPassword />} />
<Route path='/dashboard' element={<Dashboard />} />
<Route path='/reset_password/:id' element={<ResetPassword />} />
</Route>
)
);
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<Provider store={store}>
<RouterProvider router={router} />
</Provider>
</React.StrictMode>
);
and header componenet
<header>
<nav className='navbar navbar-expand-lg'>
<div className='container'>
<Link to='/' className='navbar-brand'>
<img
className='logo'
loading='lazy'
src='./images/jk_logo_horizontal.png'
alt='logo'
/>
</Link>
<div>
<ul className='navbar-nav ms-auto mb-2 mb-lg-0'>
<li className='nav-item'>
<Link to='/login' className='nav-link active' aria-current='page'>
<button className='btn btn-login'>Login</button>
</Link>
</li>
</ul>
</div>
</div>
</nav>
</header>
答案1
得分: 0
如果您从您的标志路径中移除第一个点会怎么样?
它会看起来像这样:
src='/images/jk_logo_horizontal.png'
这样,您就拥有了标志的绝对路径,无论它是否是嵌套路由。
英文:
What if you remove the first dot from your logo path?
It would look like this:
src='/images/jk_logo_horizontal.png'
This way, you have an absolute path to the logo, regardless if it is a nested route or not.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论