Deprecated as it may be, I am still determined to add an array of routes to my Vue router

In my Main.js file, I am currently fetching routes from the database using an API call. However, I've run into an issue with Vue router version 4 deprecating the addRoutes functionality. Now, I can only add one route at a time which is not ideal for me to iterate through the routes/menu list. I'm seeking advice on how to overcome this challenge.

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

Answer №1

I implement routes from my modules using the following straightforward method:

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

Previous implementation:

router.addRoutes(MyRoutes)

Your code should resemble something along these lines:

this.menuList.forEach(function(route){
  this.$router.addRoute(route)
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Style the div element with CSS

Is there a way to style a div element within an HTML document using Sencha framework? I have specific CSS properties that I would like to apply to my div. #logo{ position:absolute; top:20%; left:0%; } Below is a snippet of my Sencha code: Ex ...

Why does the page not work when I enter a certain URL with an ID parameter, and instead displays the error message "Uncaught ReferenceError: System is not defined"?

This is my "app.routing.ts": import {provideRouter, RouterConfig} from "@angular/router"; import {DashboardComponent} from "./dashboard.component"; import {HeroesComponent} from "./heroes.component"; import {HeroDetailsComponent} from "./hero-details.com ...

What could be causing the issue with the computed property not functioning correctly as the v-model on the Vuetify date

When working with Vuetify's v-date-picker, I customized a reusable component and implemented props to allow the parent component to set the value in the child component. To enable changes to these props, I created a computed property in the child comp ...

The setTimeout function appears to be malfunctioning

I've encountered an issue where a webpage keeps scrolling endlessly without stopping. I've attempted to terminate it by using the exit function, but unfortunately it's not working. Does anyone have a solution for this problem? ...

How to Call a Nested Object in JavaScript Dynamically?

var myObj = { bar_foo : "test", bar : { foo : "hi there"; }, foo : { bar : { foo: "and here we go!" } } } How can we achieve the following: var arr = [["bar", "foo"], ...

How to incorporate the b-form-datepicker component in a b-dropdown using BootstrapVue

Currently, I am developing a dropdown menu date selector that offers the user the flexibility to input a range of dates or choose from a set date range options. However, I encountered an issue with integrating b-form-datepicker within a b-dropdown. Wheneve ...

The Discord.js command outright declines to function

I'm having trouble with a code that I'm working on. The goal is to create a command that is enabled by default, but once a user uses it, it should be disabled for that user. However, when I try to execute the code, it doesn't work at all and ...

Creating three search fields in Vue.js with computed properties is a powerful and efficient way to filter

I am struggling to implement a search feature with three fields - one input and two selectors. I was able to get it to work with two fields, but adding the third is causing issues. I could really use some guidance on this. computed: { ...

Experiencing memory issues with small programs in Node on macOS?

Currently, I am in the process of learning JavaScript and making use of node to execute JS programs directly from the terminal using this command: node program1.js The issue that I am encountering involves a simple JavaScript program that is supposed to ...

Is there a way to dynamically access BEM-style selectors using CSS modules?

For instance, I have this specific selector in my App.module.css file: .Column--active I want to access this selector from the App.js file in React using CSS modules. After importing all selectors from the CSS file as import styles from './App. ...

Angular2 scripts are failing to load in the web browser

Setting up my index page has been more challenging than I anticipated. Take a look at my browser: https://i.stack.imgur.com/L4b6o.png Here is the index page I'm struggling with: https://i.stack.imgur.com/Op6lG.png I am completely stumped this tim ...

Is it possible for JavaScript code to access a file input from the terminal?

When I run the command cat input.txt | node prog.js >result.txt in my terminal, I need to use an input file. This is my code: var fs = require('fs'); var str = fs.readFileSync('input.txt', 'utf8'); str.replace(/^\s* ...

incorporating a timer into a JavaScript game

I am currently working on a memory card game where I want to include a stopwatch feature. I'm looking to display the time elapsed from when the user selects the first card until they win. However, I am struggling with keeping the stopwatch running smo ...

Vue.js is throwing an error stating that a component template must have only one main element

I am currently troubleshooting an issue with file upload functionality, but I am unsure of the specific error. In an attempt to track changes, I have been using console logs. Upon running $ npm run watch, an error message is displayed: "Webpack is watchi ...

How can I verify a user's role in Vuex?

I'm currently working on verifying the user role during login in Vue.js. Utilizing Vuex for state management, my goal is to redirect users with role = 1 (admin) to the admin panel and those with role = 0 (user) to the user dashboard. However, I seem t ...

Utilizing Vue and routes to streamline communication between components

Recently delving into Vue.js, I've hit a roadblock. My goal is to pass information from one component to another through routes. For example, when navigating from 'teacher' to 'student list' and then selecting the first student, i ...

"Collaborative Drive" assistance within Google Apps Script

Currently, I am developing a JavaScript tool within Google Apps Script to analyze various document properties such as the validity of links and correct permissions settings. To achieve this, I have been utilizing the API outlined in https://developers.goog ...

Setting both 'a' and 'href' attributes to an existing list: best practices

I am struggling with updating my navigation bar so that the first item becomes a clickable link instead of just a list item. I attempted to add a link to the existing list item, but I think I should be using the .setAttribute method in order to achieve t ...

Troubleshooting Date Problems in Angular

When using the HTML5 date picker, I have encountered an issue where after choosing a date, the ng-model displays an older date instead of the current one selected. <input type="date" ng-model="dateModel" /> For example, when selecting the current d ...

What is the best way to ensure that the input submit element is only visible after the input checkbox element has been checked?

For a school project, I am attempting to create a simulated reCAPTCHA box. Although my code is complete, I would like the functionality to submit your response from the input fields above only after selecting the reCAPTCHA checkbox. Upon researching, it ap ...