logo

该视频仅会员有权观看

立即开通课程「Next.js 前端应用开发实践」权限。

¥
199
/ 年

在 Next.js 应用里以程序方式改变路由(useRouter)

  • To change application routes programmatically, the useRotor hook from the NGGS framework, belonging to the next navigation package, can be used.
  • Use useRotor to declare a rotters object that allows altering routes.
  • By removing the surrounding Link component and binding an onClick event to a div element, the rotters.push method is executed to navigate to a different page (homepage indicated by '/').
  • However, this leads to an error because the event handler can't be passed to the client in server-side components found in the app directory.
  • To resolve this, convert the component to a client-side component using a useClient directive.
  • After conversion, testing in the browser by opening the "About" page and clicking on the app's name in the header successfully navigates using rotter.push to the homepage.