我的 Next.JS 应用程序 (404.js) 中有一个自定义 404 页面。在页面上,我想显示类似 的消息The route <strong>/not-a-route</strong> does not exist
,但是当使用 Next.js 的useRouter()
和 时router.pathname
,路由器认为我已打开/404
,而是显示The route <strong>/404</strong> does not exist
。如何在自定义 404 页面中访问我所在的实际路线?
export const NotFound: React.FC = () => {
const router = useRouter();
console.log(router.pathname) // prints /404
return (
<Layout title='Oops! Something went wrong!'>
<div className={styles.container}>
<h1>Oops! Something went wrong!</h1>
<p>
The route <strong>{router.pathname}</strong> does not exist.
</p>
</div>
</Layout>
);
};
您可以访问redirected route
as router.asPath
。尝试:
import {useRouter} from 'next/router'
export const NotFound: React.FC = () => {
const router = useRouter();
console.log(router.asPath)
return (
<Layout title='Oops! Something went wrong!'>
<div className={styles.container}>
<h1>Oops! Something went wrong!</h1>
<p>
The route <strong>{router.asPath}</strong> does not exist.
</p>
</div>
</Layout>
);
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句