在Next JS < 13中,我们有router.events
,在_app.js
中,我们可以添加这样的效果
import { useRouter } from 'next/router'
import NProgress from 'nprogress'
const Main = ({ Component, pageProps }) => {
const router = useRouter()
useEffect(function nprogressOnRouteChange() {
router.events.on('routeChangeStart', NProgress.start)
router.events.on('routeChangeError', NProgress.done)
router.events.on('routeChangeComplete', NProgress.done)
return () => {
router.events.off('routeChangeStart', NProgress.start)
router.events.off('routeChangeError', NProgress.done)
router.events.off('routeChangeComplete', NProgress.done)
NProgress.remove()
}
}, [])
...
}
字符串
但是现在useRouter
已经被移动到next/navigation
,钩子返回的对象不再有任何事件属性,我错过了什么吗?
我尝试在新的layout.tsx
文件中从next/router
导入Router
,但事件也不起作用
import Router from 'next/router'
export default function Transition({ children }) {
useEffect(() => {
Router.events.on('routeChangeStart', () => {
console.log('start')
})
}, [])
...
}
型
1条答案
按热度按时间3yhwsihp1#
字符串