yann_yinn
yann_yinn

Reputation: 144

how to extend Routes with a Nuxt module?

I want to add routes to my application with a custom module. According to the documentation, there is extendRoutes on the modulesContainer class for that. But my 'extendRoutes' methods is not called in the code below. What am i missing here ?

module.exports = function Gustave() {
  this.nuxt.hook('build:before', () => {
    const routes = runGustave()
    this.options.generate.routes = [...routes, ...this.options.generate.routes]
    console.log('hello world') // this code is called as expected
    this.extendRoutes = function(routes, resolve) {
      console.log('extendRoutes is called !') // this code is not called
      routes.push({
        name: '_gustave',
        path: '/_gustave',
        component: resolve(__dirname, 'components/hello.vue')
      })
    }
  })
}

Upvotes: 1

Views: 3745

Answers (2)

Farzad.Kamali
Farzad.Kamali

Reputation: 593

the answer of @danielkelly_io is correct only to make it more clear:

export default function (moduleOptions) {
  const options = mergeObjects({}, localOptions, moduleOptions);
  ...
  this.options.router.extendRoutes = (routes, resolve)=>{
    routes.push(
      {
        name: 'whatever',
        path: '/whatever',
        component: resolve(__dirname, '@/whatever/whatever.vue'),
      }
    )
  }

Upvotes: 2

danielkelly_io
danielkelly_io

Reputation: 61

the extendRoutes function is actually a method on the router property. You can define it like so: this.options.router.extendRoutes = (routes, resolve)=>{}

Upvotes: 2

Related Questions