5

This is my Main.js. I'm fetching routes from the database through an API call but Vue router version 4 deprecated addRoutes functionality. So now I can add only one route at a time. I don't want to add routes by iterating the routes/menu list. Please share your thoughts.

new Vue({
    store,
    router,
    render: h => h(App),
    beforeMount() {
      if (this.menuList.length) {
        this.$router.addRoutes(this.menuList);
      }
    },
    computed: {
      ...mapGetters({
        menuList: "menuStore/menuList"
      })
    },
    
  }).$mount("#app");
Jithin Vijayan
  • 307
  • 5
  • 19
  • 1
    Where did you get that addRoute is deprecated? I didn't find mentioned it in docs https://router.vuejs.org/api/interfaces/router.html#addroute – Luckylooke Aug 16 '22 at 10:52
  • @Luckylooke for vue router 3, `router.addRoutes` is marked deprecated [here](https://v3.router.vuejs.org/api/#router-addroutes). In 4 this method is no longer there. – tinystone Jul 18 '23 at 02:24

1 Answers1

1

To add routes from my modules I use this simple code:

MyRoutes.forEach(function(route){
    router.addRoute(route);
})

Old code:

router.addRoutes(MyRoutes)

So you code should be something like:

this.menuList.forEach(function(route){
  this.$router.addRoute(route)
});
Uncoke
  • 1,832
  • 4
  • 26
  • 58
  • But this menu list will contain more than 10 menu items with more than 3 sub-menu items in each menu. How can we handle this? – Jithin Vijayan Jun 22 '21 at 06:43