0

New to vuejs, so there is a constant router object

const constantRouterMap = [{
a,b,c }]

and imported to Router object

new Router({
  mode: 'history',
  ...
  ......
  routes: constantRouterMap
})

And I have also added routers in a js file

import router from '@/router'
.......
.....
router.addRoutes(dynamicRouters)

Then I tried to get all routes with

router.options.routes 

or

getRoutes()

But this only gives me 3 routes from constantRouterMap . How can I get all routes included the routes I created dynamically ?

1 Answers1

0

Are you sure you're calling getRoutes() after you added the additional routes? Are you getting any errors? Are any of the the new routes overwriting existing routes?

Working code:

const router = new VueRouter({
  routes: [
    {
      path: '/foo',
      component: { template: '<div/>' }
    }
  ]
})

router.addRoute({
  path: '/bar',
  component: { template: '<div/>' }
})

// Log the paths of each route
console.log(router.getRoutes().map(r => r.path))
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
Decade Moon
  • 32,968
  • 8
  • 81
  • 101
  • Mooon, could you please check problem of mine https://stackoverflow.com/questions/72686564/dynamically-registered-route-is-not-working-when-it-is-just-pushed – EaBengaluru Jun 21 '22 at 04:20