Vue router addRoutes deprecated but I want to add array of routes to Vue router

Viewed 1131

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");
1 Answers

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)
});
Related