NextJS TypeError: Cannot read property 'pathname' of undefined

NextJS TypeError: Cannot read property 'pathname' of undefined

我正在将整个应用程序从 React 迁移到 Next。我解决了大部分问题,但我在路由方面遇到了一些问题。

我用 useRouter 替换了 useHistory,因为我们不再使用 react-router 包,我在页面上收到最后一个错误

我不知道如何使用 next routing 解决它,最后一天都在疯狂地研究它。该位置应 return 页面的当前 URL

使用 useRouter 挂钩访问 router 对象。 router 有一个 pathname 属性.

useRouter

router

pathname: String - Current route. That is the path of the page in /pages, the configured basePath or locale is not included.

import { useRouter } from 'next/router';

...

const router = useRouter();
// router.pathname