Looping through Vue custom events

When I use v-for to display components, the custom event update emitted by the child component is not triggered. However, when using standalone components, everything works fine.

I am struggling to find a solution to this issue.

Main.js

import Vue from "vue";
import App from "./App.vue";

new Vue({
  render: (h) => h(App)
}).$mount("#app");

App.vue

<template>
  <div id="app">
    <h1>It's working</h1>
    <Inner v-on:update="this.do" />
    <h1>It's NOT working</h1>
    <Inner v-for="n in [1, 2, 3]" :key="n" v-on:update="this.do" />
  </div>
</template>

<script>
import Inner from "./Inner";

export default {
  name: "App",
  components: { Inner },
  methods: {
    do() { alert(1); },
  },
};
</script>

Inner.vue

<template>
  <button v-on:click="this.do">CLICK ME</button>
</template>

<script>
export default {
  name: "Inner",
  methods: {
    do() { this.$emit("update"); },
  },
};
</script>

Answer №1

execute is a reserved term in JavaScript, consider choosing an alternative name for the functions without utilizing this to interact with the element. Take a look at this example:

const Internal = Vue.component('Internal', {
  template: '#Internal',
  methods: {
    selected() { this.$emit("refresh"); },
  },
});

new Vue({
  el:"#application",
  components: { Internal },
  methods: {
    selected() { alert(1); },
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<template id="Internal">
  <button v-on:click="selected">CLICK HERE</button>
</template>

<div id="application">
  <h1>It does not function as expected.</h1>
  <Internal v-for="n in [1, 2, 3]" :key="n" v-on:refresh="selected" />
</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

Update the message displayed in the user interface after the view has been fully rendered in an Express application, following the execution of asynchronous

I have created a simple express app that reads files from a directory, renames them, zips the files asynchronously, and then renders another view. The file reading and renaming are done synchronously, while the zipping part is handled asynchronously. My cu ...

Issue with navigation links on HTML website not functioning as expected

I would like the dropdown menu items to be clickable. For example: Menu item: Services Sub items: - branding } These already have working links - marketing } However, when I try to replace '#' with a link for Services, it d ...

Option to publish Docker images

I'm currently trying to set up a Vue app locally inside a Docker container, but I'm facing an issue when it comes to accessing the specified port. Below is my Dockerfile: FROM node:lts-alpine RUN mkdir -p /app COPY . /app WORKDIR /app RUN npm ...

In JavaScript, use the following code to replace "(" with "(":

Is there a way to dynamically transform the string "Test(5)" into "Test\(5\)" using JQuery or Javascript? I attempted this approach, but it did not work as expected var str = "Test(5)"; str = str.replace("(","\("); str = str.replace(")"," ...

"Troubleshooting a 400 Bad Request Error in Node.js and Express for a

It seems like I must be making a silly mistake, because this should be a simple task. All I want to do is send a POST request in an Express route. This is my app.js: var express = require('express'); var path = require('path'); var f ...

Attempting to load using ajax, Chrome and jquery-mobile will modify the location of the window through setting window.location.href

I'm encountering a challenge with my mobile application where I need to redirect users to the login page on a 401 ajax call. However, it seems that jQM is attempting to load this via AJAX when the request is sent. This solution works flawlessly for s ...

What is the process for accessing my PayPal Sandbox account?

I'm having trouble logging into my SandBox Account since they updated the menu. The old steps mentioned in this post Can't login to paypal sandbox no longer seem to work. Could someone please provide me with detailed, step-by-step instructions o ...

Error: The current element cannot be clicked. Please try again

I have been attempting to trigger a click event on another button within an onClick event function. An error occurred: Uncaught TypeError: this.clickBack.current.click is not a function The React version being used is 16.8.6 constructor(props) { s ...

Strings are concatenated in object literals by creating string attributes

When working with Javascript Object literals, I am facing an issue where string attributes cannot be concatenated properly. var cart = { baseURL : "http://www.domain.com/", addURL : this.baseURL + "cart/add", deleteURL : this.baseURL + "cart/delete" ...

Safari browser is experiencing issues with the custom file upload script

My custom upload script allows users to easily select images for upload by clicking or dragging them into the designated box. A preview of the chosen image should appear, and this functionality works smoothly in Firefox and Chrome. However, I've encou ...

Encountering an issue with an AngularJS form containing ng-repeat when attempting to submit

Here is my form structure: two text inputs followed by an ng-repeat with a text input and radio button inside. <form class="form-horizontal" id="myForm" name="myForm"> <div class="form-group"> <label for="name" class="co ...

Error message: Vue warning - The prop "disabled" must be a boolean type, but a function was provided instead

I have this code snippet that I am currently using <v-list-item> <v-btn @click="onDownloadFile(document)" :disabled=getFileExtention >Download as pdf</v-btn > < ...

Transferring data between two screens within an Ionic app by harnessing the power of angularJS

As a beginner in Ionic and Angular development, I am currently facing an issue with my Ionic application. I have two pages - one with drop-down selects and a submit button, and another page to process the user's choices and display the results. The pr ...

Scrolling to the next div will automatically align it in the center of the screen for the user

I am in the process of creating a single-page website and I would like to stack multiple divs on top of each other, all approximately 400px in size (with variations). Instead of using a smooth scroll, I want the page to jump to the next div and have it cen ...

NodeJS Multer for handling multiple form fields

Dealing with Multer for multiple fields can be a challenge. I am in need of uploading images using the file field for one image, as well as utilizing "Summernote" to upload another image. All of this needs to be handled within one controller. How should ...

When generating a docx file with html-docx-js, it lacks the capability to incorporate external CSS class styles into the document

I have been utilizing the html-docx-js module to convert html content into docx format. However, I am facing an issue where most of my CSS is externally loaded and html-docx-js does not apply any styles. Here is a simplified code sample: const html = &ap ...

Ways to verify that a ray does not intersect the face further

My approach involves creating cubes and rectangles on my scene with userData that initially have all 6 parameters set to "false". I then cast a ray from each face and if it intersects with another object's face, I set that specific face's paramet ...

Enabling an application to be reached through two different domains for same-origin AJAX requests

Let's think outside the box for a moment and consider a scenario where I am unable to modify HTTP headers or CORS settings. This is merely a theoretical question. Here's the situation: Suppose I have an application hosted on one domain, and ...

Lookup all users in the database using search criteria in MongoDB

Currently, I am attempting to search for a user based on their name field using insomnia for testing purposes. In my schema for users, it looks like this: const userSchema = mongoose.Schema({ id: { type: String }, name: { type: String, require ...

display a dropdown menu with a default selection

Trying to pre-select a value in a dropdown from database but facing issues. Need assistance in resolving this problem. HTML <select class="form-control" ng-model="reportType.consolidationScopeId"> <option value="">Please select</option& ...