2024年7月18日 01:18
The difference between router.pathname and router.route in Next.js
- In Next.js,
router.pathnameandrouter.routeare two properties related to routing, but they have subtle differences. router.pathnameindicates the path displayed in the browser's address bar; it is the path visible to the user for the current page. For example, when you access/aboutin the browser,router.pathnamewill be/about.router.routealso represents the path of the current page, but it refers to the actual path of the page file or template. For example, if you use dynamic routing, such as/blog/[slug], even if the browser address shows/blog/hello-world,router.routewill still be/blog/[slug]. This meansrouter.routerepresents the routing pattern or file structure, rather than the actual URL path.- In summary,
router.pathnamefocuses on the actual URL path, whilerouter.routefocuses on the page file or template path, especially when using dynamic routing, where the difference is more pronounced.