Undefined method error encountered within Vue.js nested ref structure

My component structure is three levels deep and it's set up like this:

- container
  - section-1 // section1Ref
    - form-1 // form1Ref

The submit method in the container component will call the submit method in section-1 using this.$refs.section1Ref.submit().

Then, the submit method within the section component will trigger the submit method in the form-1 component by calling this.$refs.form1Ref.submit()

However, when I try to execute this.$refs.form1Ref.submit(), I encounter this error:

this.$refs.form1Ref.submit is not a function

Even though I have confirmed that it is defined in the methods object of the form-1 component.

This issue seems to arise specifically with a ref that is located three levels deep.

Answer №1

My experience with this code was successful, and I have included a sample of it below:

Vue.component('sec', {
  template: `
  <div>section
    <br>
    <form1 ref="form"></form1>
  </div>`,
  methods: {
    callSection() {
      this.$refs.form.callForm();
    }
  }
});
Vue.component('form1', {
  template: `<div>form</div>`,
  methods: {
    callForm() {
      console.log('am called inside form')
    }
  }
});

Vue.component('container', {
  template: `
  <div>
    <button @click="clickMe">Click me</button>
    <br>
    <sec ref="section"></sec>
  </div>`,
  methods: {
    clickMe() {
      this.$refs.section.callSection();
    }
  }
});

// create a new Vue instance and mount it to our div element above with the id of app
Vue.config.devtools = false;
Vue.config.productionTip = false;

var vm = new Vue({
  el: '#app'
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <container />
</div>

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

How can I activate JQUERY when an event occurs?

I am trying to create a selection box where, upon clicking an item on the left, it will shift automatically to the right. However, I am facing issues with using triggers to achieve this functionality. Here is the code I have written. <script type="te ...

Facing issues with Heroku and Express/Nodejs crashing?

I have been working on a React/Express application that I am attempting to deploy on Heroku. While trying to do so, I encountered the following errors in my logs: 2020-01-13T03:39:48.733455+00:00 heroku[router]: at=error code=H10 desc="App crashed" method ...

Guide on displaying the latest data of a composable in a Vue component

, my current project involves leveraging vue3 with the options API. I am facing a challenge where I need the test.js class to act as a composable entity. Specifically, I'm seeking for this class to seamlessly synchronize its state changes with another ...

Using nested v-for loops in Vue.js

Is there a way to access individual object items within a nested object using v-for in Vue.js? This is the data structure: flights: [ { "airline": "BA", "airport": "EBJ", "status": { "_code": "A", "_time": "2018-03-02T14:19:00Z" ...

Attempting to extract the data from SweetAlert

I've been attempting to retrieve the return value from sweetalert, but I encountered this: Promise {<pending>} >__proto_:Promise [[PromiseStatus]]: "resolved" [[PromiseValue]]:true resulting from this piece of code: var ret = swal({ ...

Utilizing JavaScript to dynamically set the height and width of a canvas based on the user input

How can I take user input for height and width values and apply them to a newly created canvas? I am able to retrieve the values, but I'm unsure how to set them as the style.height and style.width properties. var createNewCanvas = document.getEleme ...

Utilizing shared data properties in both JavaScript and SCSS within Vue

Vue.js 2 has caught my interest, especially with the single-file component structure: <template> <h1>Hello World</h1> </template> <script> export default { name: 'hello-world', }; </script> <style s ...

applying classes conditionally in Vue.js

Attempting to toggle a class of an element in Vue.js, which I am currently learning. The goal is for the disabled class to be applied when included is not true. A function called toggleClass has been created, but it doesn't appear to be working. Li ...

Having trouble with Vue component not showing updated data after axios POST request issue

Hi there, I'm facing an issue and could really use some guidance from a skilled Javascript Wizard. Here's the problem: I have a Laravel collection that I'm passing to a Vue component. Within the component, I am looping through the collecti ...

The checkbox will be automatically checked whenever there is any modification in the textbox

I'm currently working on a Grid view with a checkbox and two textboxes. What I want is for the checkbox to be automatically checked whenever there is a change in one of the textbox values, for example switching from 0 to 1. This project is being devel ...

Instructions on establishing a connection with a MongoDB server using JavaScript

Hello all, I am a complete beginner when it comes to working with mongodb and java script. I am currently trying to figure out how to establish a connection to my local mongodb instance using java script so I can retrieve a list of documents. Does anyone ...

Looking to conceal the input div without compromising the functionality in Angular 14

Here is the provided HTML code for scanning a barcoder and assigning it to the barCodeNumber variable. The onChange() function will be called once the barcode is scanned. Question: How can I hide the div on the UI while still allowing the function to work ...

Developing numerous global objects within the context of JavaScript in Rails

I have an instance object called @cameras in my cameras controller's map method and am extracting necessary values from it for my specific purpose: + @cameras = load_user_cameras(true, false) + @map_data = [] + @cameras.each do |camera| + ...

Creating a scrolling effect similar to the Nest Thermostat

After researching countless references, I am determined to achieve a scrolling effect similar to the Nest Thermostat. I came across this solution on this JSFiddle, but unfortunately, the parent element has a fixed position that cannot be utilized within my ...

Is it feasible to access a service instance within a parameter decorator in nest.js?

I am looking to replicate the functionality of Spring framework in nest.js with a similar code snippet like this: @Controller('/test') class TestController { @Get() get(@Principal() principal: Principal) { } } After spending countless ho ...

What does [] represent in the context of the [].forEach.call() method?

I'm a bit confused about the meaning of [] in the code snippet below. Is it just indicating the most recently declared array? Can someone provide clarification? In this particular example, we have two arrays named racersList and volunteersList stored ...

Advantages of opting for bin files instead of .js files with express-generator

Starting a Node.js project with Express typically involves using express-generator. Once the project is created, your file structure will resemble this: . ├── app.js ├── bin │ └── www ├── package.json ├── public │ ├ ...

Angular 6: Endlessly Scroll in Both Directions with Containers

Does anyone know of a library for angular 6 that allows for the creation of a scrollable container that can be scrolled indefinitely in both directions? The content within this container would need to be generated dynamically through code. For example, ...

Displaying a modal in Vue by clicking and populating it with information retrieved

Working with JSON data: "8":{ "name": "Dog", "age": 2, "showModal": false }, "9":{ "name": "Cat", "age": 7, "showModal": false }, Using v-for loop to display the data: <div v-for="(animal, index) in animals" :key="index"> ...

Setting innerHTML does not affect the content of an SVG element

I am currently working on an Angular 7 application and I need to dynamically update the text value based on a dropdown selection. For example, if the id of the text element is 10, then I want to change the text from 'hi' to 'hello'. T ...