如何使用Nuxt模块扩展路由?

时间:2019-01-02 15:41:15

标签: nuxt.js

我想使用自定义模块将路由添加到我的应用程序。根据{{​​3}},在modulesContainer类上有extendRoutes。但是我的“ extendRoutes”方法没有在下面的代码中调用。我在这里想念什么?

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')
      })
    }
  })
}

1 个答案:

答案 0 :(得分:0)

extendRoutes函数实际上是路由器属性上的一种方法。您可以这样定义:this.options.router.extendRoutes = (routes, resolve)=>{}