Top method for transferring the result of a function to descendant components in Vue

In my parent component, I have a data object named config structured like this:

data() {
   return {
       config: {
            Groups: [
              {
                name: "A",
                Types: [
                  { mask: 1234, name: "Alice", type: 1},
                  { mask: 5678, name "Bob", type: 1},
                ]
              },
              {
                name: "B",
                Types: [
                  { mask: 9876, name: "Charlie", type: 2},
                  { mask: 5432, name "Drake", type: 2},
                ]
              }
            ],
        },
        Defaults: {
          dummyBoolean: false,
          dummyNumber: 1
        }
      }
   }
}

Within this component, there are 2 child components for which I aim to pass the Types array from the config data based on each child component's name.

My initial approach involved creating separate computed functions for each child component, but I'm now seeking a more efficient solution, considering the potential addition of more child components.

  1. One solution I attempted was to have a single computed function that takes the component's name as an argument, but encountered an error (dataSender is not a function)
      computed: {
        dataSender: function(groupName) {
          let array= []
          this.config.Groups.forEach( element => {
            if (element.name === groupName) array = element.Types
          });
          return array
        },
      }
  1. Another approach involved placing the function in methods and passing it as a prop to the child components, but this resulted in passing the function itself rather than the desired array.

If anyone has a solution for this issue, I would greatly appreciate the assistance.

Answer №1

When dealing with computed properties that do not accept parameters used in the calculation, an alternative approach is to utilize a method. Consider implementing a method similar to the following:

 methods: {
        retrieveData: function(groupName) {
          let data= []
          this.config.Groups.forEach( item => {
            if (item.name === groupName) data = item.Types
          });
          return data
        },
      }

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

After clicking, revert back to the starting position

Hey there, I have a question about manipulating elements in the DOM. Here's the scenario: when I click a button, a div is displayed. After 2 seconds, this div is replaced by another one which has a function attached to it that removes the div again. ...

Altering CSS styles through JavaScript or jQuery

Exploring Options After investigating the use of .css() to manipulate CSS properties of specific elements, I came across a website showcasing the following CSS: body, table td, select { font-family: Arial Unicode MS, Arial, sans-serif; font-size: ...

Experiencing an issue with Handsontable: Every time I try to run the countRows method, all I get is "undefined."

Greetings everyone! I appreciate you taking the time to read about my issue. I've been using the handsontable library for a week now and have encountered some difficulties with certain methods. Within a function, I have the following code: count = $ ...

"Implementing Vue mousemove functionality only when the mouse button is pressed

Is it possible to initiate a mouse movement only after the element has been clicked first? I am exploring this functionality for an audio player timeline. .player__time--bar(@mousedown="setNewCurrentPosition($event)") .slider(role="slider" aria-valuem ...

Which is better for creating contour graphs: JavaScript or PHP?

I need to create a scientific graph for a web application that displays temperature data based on time and depth coordinates. You can view an example here : I have time and depth coordinates, and I want to represent temperature using color in the graph, ...

Consistently navigating back to the Home Page through AJAX

Can anyone assist me in resolving my issue? I have three files: index.php, process.js, and redirect_process.php. The index.php serves as my homepage with a form embedded in it. Whenever the submit button is clicked, the process.js script is triggered to v ...

My Javascript code initiates an AJAX request before any other operations are executed

I am facing an issue with my Flask application and client-side JavaScript. I am trying to send an object using AJAX from my JavaScript to the Flask application running on the server side. However, the AJAX request is being sent before the rest of my JavaSc ...

What could be the reason for PassportJS in Node failing to clear the session upon logout?

I am encountering an issue with my system not successfully logging out using PassportJS. The logout route appears to be triggered, but the session is not being removed as intended. I would like it to return a 401 error if the user is not logged in on a spe ...

Using jQuery and JSON data to dynamically populate a second dropdown menu with filtered options

I am working on a form with two drop-down menus and a text box. The first drop-down contains static values, while the second drop-down is populated dynamically from a JSON array. My goal is to filter the options in the second drop-down based on the selecti ...

Is it possible to perform a PHP redirect after the headers have been

I am encountering an issue with a function that needs to redirect the page once a variable is set. The challenge arises from the fact that this function is located at the bottom of the PHP page. As a result, I have already displayed a significant amount ...

button that takes you back to the top of a mobile website

I want to customize the Scroll To Top button so that it only appears once the user begins scrolling down, rather than always being visible even when at the top of the page. Just a heads up, I don't have much experience with JavaScript, so I might need ...

Sophisticated filter - Conceal Ancestry

Check out this snippet of my HTML: <td> <a class="button" href="#"> <input id="download">...</input> </a> <a class="button" href="#"> <input id="downloadcsv">...</input> </a> </td> I am ...

Unable to set initial values for Select Option in Redux-Form

I have implemented an update form using redux-form, where the form value is initialized using initialValues. For example: <DataEdit initialValues={ Data }/> The form in the DataEdit page looks like this: <Field name="Data.taxTitle" comp ...

Leverage a JavaScript plugin from the node modules directory within your Vue.js project

Is there a way to import Cal-HeatMap (https://www.npmjs.com/package/cal-heatmap) into my project after saving it with npm install? This is the method I attempted: <script> import calHeatmap from 'cal-heatmap' export default { na ...

Securely transmit ID values through ajax calls

Picture this: I have a website where users' profile pages are accessed through addresses like the following: http://www.samplewebsite.com/profile.php?p=123 Now, I want to be able to block or perform another action on a user when I click a button. To ...

"Enhance your website with autocomplete feature using the power of jQuery 1.4.2 and jQuery UI 1

Struggling to make jQuery autocomplete work. Despite searching for examples, most seem outdated. Modeled after a good example from the jQuery UI site but still can't get it to display data. My JSON data source is accessed via URL. [{ "pk": 1, "mo ...

Order JSON Array Using LoDash Library

Recently I encountered a JSON array with the following structure: var json = [ { key: 'firstName', value: 'Bill' }, { key: 'lastName', value: 'Mans' }, { key: 'phone', value: '123.456.7890&apo ...

Node.js Multer encountering undefined req.file issue when handling multiple file uploads

FIXED: ( NO req.file ) ( YES req.files ) My project requires the ability to upload multiple files. If single image uploads are working but multiple image uploads aren't (uploading to files), I need req.file.filename in order to write the image path ...

React - triggering a router link action before the link is assigned a value

In this scenario, the issue arises because the URL changes before the link receives a value. The state is being received directly through the component but by the time it happens, the component has already been clicked. This results in the value being as ...

A guide to setting defaultValue dynamically in React Hook Form

Presently, I am facing an issue with editing the Product page. The props values are fetched through an API and sent from the parent component. However, I am struggling to set this value to my Datepicker input. This is because the defaultValue function from ...