【发布时间】:2020-12-27 19:56:52
【问题描述】:
我在 Next.js 中收到此错误:
错误:提供的“href”(/subject/[subject])值缺少要正确插值的查询值(主题)。阅读更多:https://err.sh/vercel/next.js/href-interpolation-failed`。
我有一个动态页面设置为/subject/[subject].tsx。现在在我的导航中,我有:
<Link href={'/subject/${subject}'} passHref><a>{subject}</a></Link>
当我使用不同的路径访问页面时它工作正常,但是当我按下页面上的按钮时,它会引发错误,我想这是因为组件重新呈现。如果您转到错误页面,它会显示:Note: this error will only show when the next/link component is clicked not when only rendered.
我试图寻找解决方案,我尝试过这样做:
<Link href={{pathname: '/subject/[subject]', query: {subject: subject}}}></Link>
但没有任何改变。我阅读了文档,似乎 as 道具只是一个不再使用的可选装饰器,所以我看不出它对我有什么帮助。
【问题讨论】:
-
会不会是语法问题?
<Link href={`/subject/${subject}`} passHref><a>{subject}</a></Link>适合我(带有反引号而不是单引号的字符串)。 -
不是因为本站代码格式的原因我使用了单引号,在代码中我确实使用了反引号。
标签: javascript reactjs next.js