Reference to a method from a child component in Vue returns an undefined method when using v-for loop

I am facing an issue with a child component and a button that triggers a method within it:

      <div v-for="(i,index) in user.usertype.max_user" :key="index" class="d-flex w-50">
        <vue-user-profiles  ref="userProfileComponent"></vue-user-profiles>
      </div>
        <button @click="click">Click</button>

This code snippet renders the specified component multiple times based on usertype.max_user value.

    <template>
  <table class="table">
    <thead>
      <tr>
        <th scope="col">Name</th>
        &td>
          <input
            v-model="userprofile.name"
            type="text"
            class="form-control"
            placeholder="e.g. Max"
          />
        </td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Age</th>
        &td>
          <input
            v-model="userprofile.age"
            type="text"
            class="form-control"
            placeholder="e.g. 37"
          />
        </td>
      </tr> ...

Within this child component, I have a method that currently logs dummy text, but ideally, I would like to retrieve all the user inputs entered here (for each rendered form).

I am attempting to call this method from the parent component where I looped to create the vue user profiles as shown below:

click: function() {
  this.$refs.userProfileComponent.functionInsideChildComponent();
}

The error message I encountered is:

Error in v-on handler: "TypeError: this.$refs.userProfileComponent.loger is not a function"

found in

---> <VueProfileEdit> at resources/js/components/VueProfileEdit.vue

Any assistance or suggestions would be greatly appreciated. I am currently stuck on this problem, thank you in advance.

Edit: For clarification, I can make it work manually by using this.$refs.userProfileComponent[0].functionInsideChildComponent(); however, this only targets the first component and not all of them. What I need is to loop through all the vue user profile components upon button click, store the data with a variable indexer to differentiate between different forms, and send this data to the parent component for further processing.

Answer №1

After some troubleshooting, I managed to successfully run the loop using the following code:

this.$refs.userProfileComponent.forEach(item => item.functionInsideChildComponent());

If you need to include multiple statements or functions within the loop, simply adjust it like this:

this.$refs.userProfileComponent.forEach(item => {
item.functionInsideChildComponent();
console.log("Greetings");
});

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

Find the nearest element with a specific class using jQuery

I am currently using jQuery version 1.12.4 for the purpose of retrieving the value from the closest element with a specific class selector. Unfortunately, I am encountering difficulty in selecting the closest element as desired. $(function() { $("[cla ...

What is the best way to ensure a jQuery UI slider recognizes when the mouse is released after being clicked?

I have integrated the jQuery slider into my application and am facing an issue with triggering an event to update the database upon releasing the slider. Currently, I am using the following code snippet: $('.ui-slider-handle').mouseup(function () ...

Bidirectional updates in AngularJS with CSS styling

On the backend, certain HTML elements store their position and size persistently and retrieve them when the page loads. These elements can be dragged and resized by users, with any updates needing to be saved on the backend for consistency across sessions. ...

What can I do to condense the length of b-dropdown-item-button or make the text wrap instead?

DropDown Currently, I am utilizing Bootstrap-vue. My goal is to ensure that the b-dropdown-item-button wraps the text it displays. Is there a recommended approach for achieving this? <template> <b-dropdown menu-class="py-1" text="··· Action ...

Modifying dat.gui to reflect changes made to the selected object

check out this working code I am currently exploring ways to update or refresh dat.gui in order to reflect the changes I make to my selection. My main objective is to generate random cubes and then manipulate a single cube by rotating, scaling, changing p ...

Passing component properties using spaces in VueJS is a simple and effective

I am encountering an issue where I want to pass component props from my view, but I am facing a challenge due to the presence of a space in the value. This causes Vue to return the following error: vendor.js:695 [Vue warn]: Error compiling template: - inva ...

Struggling with setting values in AngularJS?

Can you assist me in passing data from: (Page 1 -> Module 1 -> Controller 1) to (Page 2 -> Module 2 -> Controller 2) For example: Module reports - Controller ReportsCtrl //Page 1 <html lang="en" class="no-js" ng-app="reports"> <a n ...

Integrating a Find Pano functionality into a Kolor Panotour

I used a program called Kolor to create a panorama. Now, I am attempting to integrate the "find pano" feature, which involves searching through the panoramic images for display purposes. I have come across an HTML file that contains the search functionalit ...

Retrieving the current value of the selected option using JQuery

I am working on a feature where I have different quantities in selects after each button click. <button type="button" class="btn btn-sm btn-primary" id="addtocart2" >Button1</button> <select id="quantity1" class="ml- ...

What is the method for writing to an HTML file with the use of expressjs?

Alright, I have an interesting idea here. I am looking for a way to allow users to push a button and have a new p element permanently added to the HTML file. This means that even after reloading the page, everyone should be able to see this new element. An ...

Encountering an error stating "ngbCollapse" is not a recognized property of "div" after relocating components to a sub module

Error compiler.js:215 Uncaught Error: Template parse errors: Can't bind to 'ngbCollapse' since it isn't a known property of 'div'. ("][ngbCollapse]="isHidden" In the process of organizing my code, I encountered an issue ...

Overriding the Ajax URL

During an AJAX request in a Rails app triggered by onchange event, I am encountering an issue with the URL being called. The function for the request is a simple PUT operation: function quantityChange(id, val) { $.ajax({ url: 'cart_items/ ...

Ways to adjust the position of a DIV based on its index value

I'm currently working on a unique project that involves creating a triangular grid using HTML and CSS. The challenge I am facing is offsetting each triangle in the grid to the left by increasing amounts so they fit seamlessly next to one another. Righ ...

Tab buttons that switch between different sections with just a click

Forgive me if this seems like a simple coding issue, but I struggle with javascript and need some guidance... I have a setup where two buttons (blue and yellow) toggle between different content divs. On another part of the page, there are also two buttons ...

The appearance of an unforeseen * symbol caused a

Having issues with this particular line of code, import * as posenet from '@tensorflow-models/posenet' The error 'Uncaught SyntaxError: Unexpected token *' keeps popping up, I have the latest version of Chrome installed and I've ...

Tips for incorporating child components when creating unit tests in Angular 2

While working on my component, I encountered an issue with the child component during unit testing. An error message is appearing stating that the child component is not defined. Any advice or solutions would be greatly appreciated. Thank you in advance. ...

What is the rationale behind not passing $scope to a service in AngularJS, and is it considered bad practice?

Is it advisable not to pass $scope to a service for certain reasons? I understand that services are intended to be reusable singletons, and passing a (potentially) large object to the service could lead to maintenance issues. However, assuming there is so ...

Validation of default values in contact forms

Obtained a free contact form online and hoping it works flawlessly, but struggling with validation for fields like "Full Name" in JS and PHP. Here is the HTML code: <input type="text" name="contactname" id="contactname" class="required" role="inpu ...

Encountering a Laravel Vue issue while attempting to implement chart.js using the Laravel-charts package

While utilizing Laravel charts from , I encountered an error message below. I observed that the error vanishes when I remove this particular line (which is associated with rendering and placed in the layout before yielding content). However, upon doing so ...

Through the implementation of JavaScript, the element's class is dynamically altered

While working on my project, I encountered an issue with implementing a change in the class of an element using JavaScript. The problem is that when I click on the home page, the last page does not get deselected. Here is my current JavaScript code: const ...