简体   繁体   English

链接组件插值错误 nextjs

[英]Link component interpolation error nextjs

I am getting this error in Next.js:我在 Next.js 中收到此错误:

Error: The provided 'href' (/subject/[subject]) value is missing query values (subject) to be interpolated properly.错误:提供的“href”(/subject/[subject])值缺少要正确插值的查询值(主题)。 Read more: https://err.sh/vercel/next.js/href-interpolation-failed` .阅读更多: https://err.sh/vercel/next.js/href-interpolation-failed`

I have a dynamic page set up as /subject/[subject].tsx .我有一个动态页面设置为/subject/[subject].tsx Now in my navigation I have:现在在我的导航中,我有:

<Link href={'/subject/${subject}'} passHref><a>{subject}</a></Link>

It works fine when I access the page with different paths but when I am pressing on a button on the page it throws the error above which I imagine is because the component rerenders.当我使用不同的路径访问页面时,它工作正常,但是当我按下页面上的按钮时,它会引发错误,我想这是因为组件重新呈现。 If you go to the page in the error it says: Note: this error will only show when the next/link component is clicked not when only rendered.如果您将 go 转到错误页面,它会显示: Note: this error will only show when the next/link component is clicked not when only rendered.

I have tried to look for a solution and I tried doing:我试图寻找解决方案,并尝试这样做:

<Link href={{pathname: '/subject/[subject]', query: {subject: subject}}}></Link>

but nothing changed.但没有任何改变。 I read the docs and it seems that the as prop is only an optional decorator that is not used anymore so I fail to see how that can help me.我阅读了文档,似乎as道具只是一个不再使用的可选装饰器,所以我看不出它对我有什么帮助。

Another possible solution could be redirect using the router.push function:另一种可能的解决方案是使用router.push function 重定向:

const myRedirectFunction = function () {

    if (typeof window !== 'undefined') {
        router.push({
            pathname: router.pathname,
            query: {...router.query, myqueryparam: 'myvalue'},
        })
    }

}


return (
  <>
    <button onClick={() => {myRedirectFunction()}}> Continue </button>
  </>

) )

It is important to include ...router.query because there if where the [dynamic] current value is included, so we need to keep it.包含...router.query很重要,因为如果包含 [动态] 当前值的位置,那么我们需要保留它。

Reference: https://github.com/vercel/next.js/blob/master/errors/href-interpolation-failed.md参考: https://github.com/vercel/next.js/blob/master/errors/href-interpolation-failed.md

You better do a null || undefined你最好做一个null || undefined null || undefined check in prior. null || undefined的检查。

@Bentasy, Like you rightly pointed out, any re-render of the page is expecting you to pass in the dynamic route params again. @Bentasy,就像您正确指出的那样,页面的任何重新渲染都希望您再次传入动态路由参数。 The way I avoided page re-rendering with subsequent clicks on the page was to replace Link tag on the page with a label tag.我避免在随后点击页面时重新呈现页面的方法是将页面上的Link标记替换为label标记。 I was using the Next Link tag to navigate between the tabs on this image which generated this same error.我正在使用 Next Link 标记在此图像上的选项卡之间导航,这会产生相同的错误。 So I replaced the Link tags with label tags and the error was solved.所以我用 label 标签替换了 Link 标签,错误就解决了。 页面上的选项卡。

I got the same issue when trying to redirect user to locale.尝试将用户重定向到语言环境时遇到了同样的问题。 I did it in useEffect .我在useEffect中做到了。 After investigate I discovered that on first render router.query is empty, so it's missing required field id .经过调查,我发现第一次渲染router.query是空的,所以它缺少必填字段id I fix it by using router.isReady我使用router.isReady修复它

 export const useUserLanguageRoute = () => { const router = useRouter() useEffect(() => { const { locales = [], locale, asPath, defaultLocale, pathname, query, isReady // here it is } = router const browserLanguage = window.navigator.language.slice(0, 2) const shouldChangeLocale = isReady && // and here I use it locale.== browserLanguage && locale === defaultLocale && locales.includes(browserLanguage) if (shouldChangeLocale) { router,push( { pathname, query, }, asPath: { locale, browserLanguage } ) } }, [router]) }

I was having the same problem, this is how I solved it.我遇到了同样的问题,这就是我解决它的方法。

While pushing something to the router, we need to give the old values in it.在将某些内容推送到路由器时,我们需要在其中提供旧值。

In that:在那里面:

const router = useRouter()

router.push({
   ...router,
   pathname: '/your-pathname'
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM