Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Routes in NuxtJS

Tags:

nuxt.js

I am wondering how to rename routes in nuxtjs.

In particular, I have a folder called pages with a number of files in that folder (like about-us, contact-us, etc). However, I do NOT want the route to be www.mysite.com/pages/about-us, but rather www.mysite.com/about-us. That is to say, I want to remove pages from the route.

Any idea how I can do this?

like image 232
Moshe Avatar asked Oct 23 '25 17:10

Moshe


2 Answers

To customize the route path, simply, you can use extendRoutes function in the nuxt.config.js and edit the path property like this:

export default {
  ...
  router: {
    extendRoutes(routes, resolve) {
      return [
        {
          name: 'about',
          path: '/my-about-us', // <--- change this
          component: resolve(__dirname, 'pages/about/index.vue'),
          chunkName: 'pages/about/index'
        },
        ...
      ]
    }
  }
}

Make sure to return all of the routes (not only about page). I used console.log(routes) to know that.

Or, you can use a condition like this:

extendRoutes(routes, resolve) {
  for (const route of routes) {
    if (route.path.includes('/about')) {
      route.path = '/my-about-us'
    }
  }
  return routes;
}
like image 199
Rza Avatar answered Oct 26 '25 10:10

Rza


Refer to Routing in nuxt.js documentation page, it will automatically generate routes without pages/.

Is it possible that you have pages child folder inside the src/client/pages folder?

like image 37
V-SHY Avatar answered Oct 26 '25 12:10

V-SHY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!