6

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");
2
  • 1
    Where did you get that addRoute is deprecated? I didn't find mentioned it in docs router.vuejs.org/api/interfaces/router.html#addroute Commented Aug 16, 2022 at 10:52
  • 1
    @Luckylooke for vue router 3, router.addRoutes is marked deprecated here. In 4 this method is no longer there. Commented Jul 18, 2023 at 2:24

1 Answer 1

2

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)
});
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.