当我刷新页面时,在Next.js中收到以下错误。
在NavBar中,它是
import Link from "next/link"
import { useRouter} from "next/router"
import { useTranslation, useLanguageQuery, LanguageSwitcher } from 'next-export-i18n'
import { useEffect, useState } from 'react'
const NavBar = ({...props}) => {
const router = useRouter()
const { t } = useTranslation('navbar')
var [queryLanguage] = useLanguageQuery()
const [language, setLanguage] = useState(null)
useEffect(() => {
if(queryLanguage){
setLanguage(queryLanguage.lang)
}
},[queryLanguage])
return(
<div id="app-sidepanel" className="app-sidepanel sidepanel-hidden">
<div id="sidepanel-drop" className="sidepanel-drop">
</div>
<div className="sidepanel-inner d-flex flex-column">
<a href="#" id="sidepanel-close" className="sidepanel-close d-xl-none">×</a>
<div className="app-branding">
<Link href={{ pathname: '/', query: (router.query.lang) ? 'lang='+router.query.lang : null }} as={{ pathname: '/', query: (router.query.lang) ? 'lang='+router.query.lang : null }}>
<a className="app-logo">
<img src="/images/sa-logo.svg" alt="" width={150} height={75} />
</a>
</Link>
</div>
<nav id="app-nav-main" className="app-nav app-nav-main flex-grow-1">
<ul className="app-menu list-unstyled accordion" id="menu-accordion">
<li className={router.pathname == "/" ? "active-link nav-item" : "nav-item"}>
<Link href={{ pathname: '/', query: (router.query.lang) ? 'lang='+router.query.lang : null }} as={{ pathname: '/', query: (router.query.lang) ? 'lang='+router.query.lang : null }}>
<a className="nav-link">
<span className="nav-icon">
<img src="/icons/dashboard.png" width="28" height="28" alt="" />
</span>
<span className="nav-link-text">{t('live_view')}</span>
</a>
</Link>
</li>
<li className={router.pathname.startsWith('/settings') ? "active-link nav-item dropdown" : "nav-item dropdown"}>
<a
className="nav-link dropdown-toggle"
href="#"
id="navbarDropdown"
role="button"
data-bs-toggle="dropdown"
aria-expanded="true"
>
<span className="nav-icon"><img src="/icons/setting.png" alt="" width="28" height="28" /> </span>
<span className="nav-link-text">{t('settings')}</span>
</a>
<ul className="dropdown-menu" aria-labelledby="navbarDropdown">
<li className={router.pathname.startsWith('/settings/locations') ? "active-link" : ""}>
<Link href="/settings/locations">
<a className="dropdown-item">{t('locations')}</a>
</Link>
</li>
<li className={router.pathname.startsWith('/settings/employeetypes') ? "active-link" : ""}>
<Link className="dropdown-item" href='/settings/employeetypes'>
<a className="dropdown-item">{t('employee_type')}</a>
</Link>
</li>
<li className={router.pathname.startsWith('/settings/employeestatuses') ? "active-link" : ""}>
<Link href='/settings/employeestatuses'>
<a className="dropdown-item">{t('employee_status')}</a>
</Link>
</li>
<li className={router.pathname.startsWith('/settings/vehicletypes') ? "active-link" : ""}>
<Link href='/settings/vehicletypes/'>
<a className="dropdown-item" >{t('vehicle_type')}</a>
</Link>
</li>
<li className={router.pathname.startsWith('/settings/vehiclestatuses') ? "active-link" : ""}>
<Link href='/settings/vehiclestatuses/'>
<a className="dropdown-item" href="#">{t('vehicle_status')}</a>
</Link>
</li>
</ul>
</li>
</ul>
</nav>
<div className="app-sidepanel-footer">
<nav className="app-nav app-nav-footer">
<ul className="app-menu footer-menu list-unstyled">
<li className="nav-item">
<div className="form-check form-check-inline multi-language-switch">
{(queryLanguage) ? (queryLanguage.lang == 'en') ? <img src="/icons/tick.png" alt="" width="20" height="20" /> : '' : ''}
<LanguageSwitcher lang="en">English</LanguageSwitcher> |{' '}
</div>
<div className="form-check form-check-inline multi-language-switch">
{(queryLanguage) ? (queryLanguage.lang == 'ar') ? <img src="/icons/tick.png" alt="" width="20" height="20" /> : '' : ''}
<LanguageSwitcher lang="ar">Arabic</LanguageSwitcher>
</div>
</li>
<li className="nav-item">
<a className="nav-link" href="mailto:deepanshu@rkmarketing.net" target="_blank" rel="noreferrer">
<span className="nav-icon"><img src="/icons/contact.png" alt="" width="28" height="28" /></span>
<span className="nav-link-text">{t('contact_administrator')}</span>
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
)
}
导出默认导航栏
项目页面结构为:
pages
index.js
_app.js
settings / locations / index.js
add.js
edit / [...id].js
/ employeetypes / index.js
add.js
edit / [...id].js
我使用next-export-i18n
模块进行语言切换,因为使用next-i18next
我无法导出构建。
2条答案
按热度按时间rks48beu1#
这里的错误是,下一个Js客户端知道
/?lang=ar#
,但收到的道具中的href给出了/#
,因此下一个js由于错误的href而抛出错误。要解决这个问题,将道具数据从
/#
更改为#
应该可以。在我的错误中,我在开始时传递了
contact-us
而没有/
,链接正在工作,但Next js显示错误,它不是有效的链接,对搜索引擎和Robots不好,所以解决方法只是将其更改为/contact-us
。就这样,解决了!h5qlskok2#
TLDR如果您试图使用
href="#"
加载的页面上有任何链接,则在将查询附加到URL时会出现此错误。我刚刚在开发中遇到了同样的问题。在我的案例中:
http://localhost:3000/login?msg=abcd
时出错-因此问题的来源不可能是路由器。推送或任何东西。getServerSideProps
等的页面上事实证明,该页面本身包含一个带有
href="#"
的链接。这将触发错误。注:我试过其他锚链接,但问题似乎只出现在
href="#"
链接上。锚链接与例如href="#welcome"
工作良好。我找到了问题案例here