Selecting dynamic routes with VueJS components

My Vue instance looks like this:

new Vue({
     el: '#Application',
     router: Router,
     components: { 'ExampleDepartment', Application },
     data: function() {
          return {

          }
     }
});

In my application file, I include the template sidebar action. Inside the template, it contains the following:

<v-list-tile v-for="action in actions" :key="action.label" v-if="action.visibility == true">
     ...
</v-list-tile>

And inside, there is the code snippet below:

export default {
     watch: {
          $route: function() {
               this.getOrSetPageVisibility();
          }
     },

     methods: {
         getOrSetPageVisibility() {
               for(let index = 0; index < this.actions.length; index++) {
                    if(this.actions[index].page == this.$router.currentRoute.name) {
                         this.actions.$set(index, { visibility }, true);
                    }
               }
         }
     },

     data: function() {
          return {
               actions: [
                   {
                        label: 'Add Sample',
                        icon: 'add_circle',
                        page: 'Sample',
                        visibility: false
                   }
               ]
          }
     }
}

The problem lies in updating the menu items dynamically when the page switches. Despite executing my method through the watch as the menu changes, I encounter an issue modifying the array. The error message indicates that $set is either invalid or undefined, preventing the component from being updated upon change.

Although I can see that my method runs when the menu changes, it fails to modify the array successfully.

I am seeking a solution on how to add menu items dynamically based on the selected page.

Answer №1

You have not utilized the Vue.set method correctly.

This method can be accessed on a Vue instance using this.$set. The correct usage is demonstrated below:

this.$set(this.actions[index], 'visibility', true);

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

What is the process for updating the state value in Ngrx version 9.x?

I'm currently exploring how to assign a specific value in the most recent release of Ngrx. The documentation explains how to increase/decrease/reset values in the store, however, there are no examples provided on setting values dynamically or passing ...

Utilize Vue CLI 3 to enable popups in arcgis API JS

I've been attempting to enable popups from the ArcGIS API JS to show using the Vue-CLI 3 framework. Unfortunately, even with a simple sample code, I'm unable to make it function properly. Below is the code initially written in vanilla JS: <!DO ...

I have noticed that the SVG viewBox appears different when using Vue.js

I cannot wrap my head around the fact that my svg viewBox acts differently in html compared to vuejs. Here is my code in vue: <template> <div class="venuecontainer" style="background-color:#808040;"> <svg class=&quo ...

Adding animation to a div that is being appended can be done by using JavaScript functions and

I am looking to incorporate animation into my title div. Below is the HTML code I have so far: <div class="particles" id="tittle"> <!-- Displaying title from Firestore using JavaScript --> </div> Here is the CSS cod ...

Child component event triggers an error: "Uncaught TypeError: Unable to access property 'value' of undefined"

I'm a newcomer to React and while following the Lifting State Up tutorial, I encountered issues when attempting to modify the value from Child.js. An error is thrown stating that it is undefined. Another question I have is why it's impossible to ...

Typescript is throwing a Mongoose error stating that the Schema has not been registered for the model

I've dedicated a lot of time to researching online, but I can't seem to figure out what's missing in this case. Any help would be greatly appreciated! Permission.ts (This is the Permission model file. It has references with the Module model ...

Exploring the capabilities of Google Drive API using Requests library

I am interested in streaming a file from a remote source to Google Drive. By utilizing the request library, you can easily download files locally like so: request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png') ...

What is the best way to send multiple requests until one is successful in Node.js without causing delays?

My situation involves a function that requires a parameter and a callback. This function is responsible for making a request to an external API in order to retrieve information based on the parameter provided. The challenge arises when the remote API occas ...

Increasing/Decreasing Quantity in Shopify's Shopping Cart

I am currently in the process of creating my first custom Shopify website, but I am facing a challenge. I would like to incorporate a feature that allows for an increase or decrease in quantity using text input instead of number input (to remove the up an ...

Incorporate XHR into a JavaScript Function

Upon running the code below, a GET message is displayed in the Firebug Console. However, the variable appears as undefined, most likely due to its limited scope within the function. The objective here is to click on a table row, fetch relevant data from t ...

Obtaining a group object when the property value matches the itemSearch criteria

What is the best way to extract specific objects from a group when one of their properties has an array value, specifically using _.lodash/underscore? { "tileRecords" : [ { "tileName" : "Fama Brown", "tileGroup" : ["Polished", "Matt", ...

Unable to locate the source maps for the combination of Karma, Jasmine, TypeScript, and Webpack

In an attempt to test my TypeScript application thoroughly using Karma, Jasmine, and Webpack with code coverage, I have encountered some challenges. While I can successfully run tests, generating coverage seems to be a hurdle. The usage of karma-remap-cove ...

Create a class for the grandparent element

Is there a way to dynamically add a class to a dropdown menu item when a specific child element is clicked? Here's the HTML structure I have: <ul id="FirstLevel"> <li><a href="#">FirstLevel</a></li> <li>< ...

Is there a way to invoke an AngularJS function using JavaScript without specifying the ID parameter?

I am interested in invoking an AngularJS function without relying on the use of an id parameter. My current method for calling the AngularJS function is shown below: JS File: angular.element(document.getElementById('service_search')).scope().s ...

VueJS loop creating excessive div element

I am facing an issue where extra div is being created while displaying cards from API data, causing unnecessary space on the page. Here is my response: [ ... { "id": 6, "name": "Highway", ...

Angular - Navigate to Login Page post registration and display a confirmation message

As a newcomer to Angular, I am currently working on an Angular and Spring Boot application. So far, I have created components for user login and registration along with validation features. Now, my goal is to redirect the user to the login page upon succes ...

How to effectively handle a Lerna monorepo containing packages designed for both Vue 2 and Vue 3

I am currently working on creating a pull request to contribute to an open-source library that utilizes Lerna to manage multiple packages and npm as the package manager. Within the library, there is already support for Vue 2 through the existing package s ...

Incorporate an image into a Component template by utilizing a URL that is provided as a property of the Component in Vue and Vite

One of the challenges I faced was setting the 'src' attribute of an image in a Component's template from a property value. Initially, I attempted to do this as follows: Gallery View, where the Component is called multiple times <script s ...

Having difficulty with Bootstrap buttons functioning correctly within a React environment

I am currently working on creating a responsive navigation bar in React. One issue I am facing is that when I zoom in on the page, the links disappear and are replaced with a button that triggers a drop-down list with the links. However, when I click on t ...

Try implementing the Angular 9 Slice Pipe within your ngFor loop

Struggling to figure out how to properly utilize the Slice pipe from Angular within a for loop. Consider this code snippet: textArray = ['aaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbb', 'cccccccccccccccccccc']; <n ...