Logo 在嵌套路由上没有加载。

huangapple go评论60阅读模式
英文:

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(
		&lt;Route path=&#39;/&#39; element={&lt;App /&gt;}&gt;
			&lt;Route index={true} path=&#39;/&#39; element={&lt;Homepage /&gt;} /&gt;
			&lt;Route path=&#39;/login&#39; element={&lt;Login /&gt;} /&gt;
			&lt;Route path=&#39;/signup&#39; element={&lt;Signup /&gt;} /&gt;
			&lt;Route path=&#39;/verification&#39; element={&lt;VerifyUser /&gt;} /&gt;
			&lt;Route path=&#39;/forgot-password&#39; element={&lt;ForgotPassword /&gt;} /&gt;
			&lt;Route path=&#39;/dashboard&#39; element={&lt;Dashboard /&gt;} /&gt;
			&lt;Route path=&#39;/reset_password/:id&#39; element={&lt;ResetPassword /&gt;} /&gt;
		&lt;/Route&gt;
	)
);

const root = ReactDOM.createRoot(document.getElementById(&quot;root&quot;));
root.render(
	&lt;React.StrictMode&gt;
		&lt;Provider store={store}&gt;
			&lt;RouterProvider router={router} /&gt;
		&lt;/Provider&gt;
	&lt;/React.StrictMode&gt;
);

and header componenet

&lt;header&gt;
			&lt;nav className=&#39;navbar navbar-expand-lg&#39;&gt;
				&lt;div className=&#39;container&#39;&gt;
					&lt;Link to=&#39;/&#39; className=&#39;navbar-brand&#39;&gt;
						&lt;img
							className=&#39;logo&#39;
							loading=&#39;lazy&#39;
							src=&#39;./images/jk_logo_horizontal.png&#39;
							alt=&#39;logo&#39;
						/&gt;
					&lt;/Link&gt;
					&lt;div&gt;
						&lt;ul className=&#39;navbar-nav ms-auto mb-2 mb-lg-0&#39;&gt;
							&lt;li className=&#39;nav-item&#39;&gt;
								&lt;Link to=&#39;/login&#39; className=&#39;nav-link active&#39; aria-current=&#39;page&#39;&gt;
									&lt;button className=&#39;btn btn-login&#39;&gt;Login&lt;/button&gt;
								&lt;/Link&gt;
							&lt;/li&gt;
						&lt;/ul&gt;
					&lt;/div&gt;
				&lt;/div&gt;
			&lt;/nav&gt;
		&lt;/header&gt;

答案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=&#39;/images/jk_logo_horizontal.png&#39;

This way, you have an absolute path to the logo, regardless if it is a nested route or not.

huangapple
  • 本文由 发表于 2023年6月5日 20:30:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定