Unique title: "Implementing Unique Event Handlers with VueJS Components"

My VueJS and Buefy project begins with two distinct click actions:

  • Click on the Cyan area -> redirects to another page (Action 1)
  • Click on the Magenta area -> shows a dropdown menu (Action 2)

https://i.stack.imgur.com/AVLOS.png

However, when clicking on Action 2, it always triggers Action 1.

Below is my component code:

<MyComponent
  :projects="data"
  @click.native="actionOne()"
/>

Within my component, there is a dropdown (utilizing Buefy component) :

<p>{{ data.projects }}</p>
<BDropdown aria-role="list">
  <BButton
    slot="trigger"
    class="button"
    type="is-text"
    @click.prevent="actionTwo()"
  >
    <BIcon icon="dots-horizontal" />
  </BButton>
  <BDropdownItem aria-role="listitem">Update</BDropdownItem>
  <BDropdownItem aria-role="listitem">Archive</BDropdownItem>
</BDropdown>

I have attempted using different event modifiers like stop and prevent, but the desired behavior is not achieved.

  • stop
  • prevent

Answer №1

This issue may arise due to event bubbling. When the dropdown element is clicked, the click event travels up to the cyan area. To resolve this, you should prevent the event from bubbling for the dropdown element.

<BDropdown aria-role="list">
  <BButton
    slot="trigger"
    class="button"
    type="is-text"
    @click="actionTwo($event)"
  >
    <BIcon icon="dots-horizontal" />
  </BButton>
  <BDropdownItem aria-role="listitem">Update</BDropdownItem>
  <BDropdownItem aria-role="listitem">Archive</BDropdownItem>
</BDropdown>

<script>
export default {
    methods: {
        actionTwo(e) {
            e.cancelBubble = true
        }
    }
}

Answer №2

After some investigation, I discovered the solution to my problem. It turns out that the issue was related to a specific event from the Dropdown component (Buefy). To resolve it, I added the stop modifier to the Dropdown trigger click event and included prevent in my component.

Here is the updated solution:

<MyComponent
  :projects="data"
  @click.native.prevent="actionOne()"
/>
<BDropdown aria-role="list" @click.native.stop>
  <BButton
    slot="trigger"
    class="button"
    type="is-text"
  >
    <BIcon icon="dots-horizontal" />
  </BButton>
  <BDropdownItem aria-role="listitem">Update</BDropdownItem>
  <BDropdownItem aria-role="listitem">Archive</BDropdownItem>
</BDropdown>


Answer №3

Utilize the self modifier for optimal functionality.

<MyComponent
  :projects="data"
  @click.native.self="actionOne()"
/>

According to the documentation:

only activate handler if event.target matches the element itself
as opposed to originating from a child element
(source)

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 to Incorporate an Anchor Link into a Div as well as its Pseudo Element using CSS, Javascript, or jQuery

How can I make both the menu item and its icon clickable as a link? When either is clicked, the link should work. Here is a CodePen example: http://codepen.io/emilychews/pen/wJrqaR The red square serves as the container for the icon that will be used. ...

Utilizing the asyncData hook while performing a hard refresh in NuxtJS

After some investigation, I discovered that the asyncData hook does not get triggered when performing a hard refresh on the page. However, it is crucial for me to load important data to display on this page. I need to ensure that this data is always avai ...

trigger a p:ajax event by employing a right-click action

Currently, I am utilizing JSF and Primefaces 5.2. A peculiar observation I made is that when a commandLink is used with the onclick event and p:ajax, it creates a selection effect. <h:commandLink id="commandLink"> <p:ajax event="click"/> </ ...

Using AngularJS to iterate through JSON data

I received JSON data from my Facebook account and saved it in a file called facebook.json. Next step is to retrieve and process the data from the JSON file in my controller. app.controller('Ctrl', function($scope, $http) { $http({metho ...

Angular Alert: [$injector:modulerr] Unable to create the angularCharts module because: Error: [$injector:nomod]

Although this question has been asked multiple times, the standard solutions provided did not resolve my issue. Therefore, I am sharing my code along with the error in hopes that it will be self-explanatory. Error: Uncaught Error: [$injector:modulerr] Fa ...

Guidelines for incorporating Angular variables into jQuery scripts

I have a form with an input field that dynamically generates a list of names using Angular. My goal is to turn each name into a clickable link that will submit the form with that specific name as the input value. <form method="POST" action="/results/" ...

Changing the value of a form input name after a $scope.$watch event is activated

In my AngularJS 1.4 project, I have a form input element where I am attempting to dynamically set the name attribute. <input type="email" name="{{ctrl.name}}" class="form-control" id="email" ng-minlength="5" required> The assignment of the name att ...

My Node.js code has encountered an unexpected end of input error

const express = require("express"); const bodyParser = require("body-parser"); const ejs = require("ejs"); const mongoose = require('mongoose'); const app = express(); app.set('view engine', 'ejs'); app.use(bodyParser.url ...

Generatively generating a new element for the react application to be mounted on

I am looking to enhance my JavaScript application using React by creating a build. Currently, the application consists of just a single file structured as follows. This file essentially creates a div element and continuously changes the color of the text & ...

Error: The variable "deleted1" is not declared and cannot be used on the HTML button element's onclick

Hello, I need assistance from someone. I encountered an error message after trying to delete a row even though I have declared the button already. Error: deleted1 is not defined at HTMLButtonElement.onclick Could it be due to the script type being modul ...

Having trouble accessing news feed with jQuery due to an unexpected token error when attempting to cross domains

I am attempting to access the Yahoo News feed from a SharePoint site, but it is causing a cross-domain access issue. Despite trying various solutions found on numerous websites and blogs, I still cannot resolve the problem. (I am executing this code in the ...

The functionality of Vue.js Stylus and scoped CSS appears to be malfunctioning

I am currently utilizing stylus and scoped CSS styles with Vuetify. Despite trying to use deep nested selectors such as ::v-deep or >>&, I have not been successful in getting them to work (even after rebuilding the project due to issues with hot relo ...

A function containing a JavaScript Event Listener for better encapsulation

I am encountering an issue with an event listener inside a function where both the listener and emitter are within the same function scope. Upon triggering the event, a variable is supposed to increment. However, when I call the function multiple times, t ...

Looking for assistance with my MEAN stack To Do App project development

For a test at an enterprise, I have been tasked with creating a "to do APP" using Node js, Express, MongoDB & Angular Js. This is new territory for me as I have never worked with the MEAN Stack before but I am excited to explore it! The base project has al ...

Having difficulty in choosing and displaying the dropdown values

Below is the code snippet: WebElement pooldropdown=driver.findElement(By.xpath("/html/body/section/section[2]/div[1]/select")); Select sel = new Select(pooldropdown); List<WebElement> list = sel.getOptions(); System. ...

How can I search across different fields within a single collection using meteor-autocomplete?

I have implemented mizzao/meteor-autcomplete to retrieve matching items from a MongoDB collection based on user input. While I can successfully search for items in one field, I am facing difficulty searching multiple fields within the same collection. My ...

Trouble with Fetch in JS and PHP not sending parameters

Having trouble getting a PHP function to accept data from JavaScript, despite the PHP class working elsewhere in the application. The getTopFive() function works fine, but data insertion isn't happening as expected. Below is the code snippet along wit ...

What is causing the color to be replaced by the latest selection?

I'm having trouble drawing three lines with different colors. They all end up being the same color, which is the last color specified in my code snippet below: function initObject() { var lineLength = 10; geometry = new THREE.Geometry ...

While trying to set up a development server in Firebase, I mistakenly deleted my build folder

I recently encountered an issue with my Firebase project. While trying to set up a development server for my existing React web app that is already in production, I ran into some unexpected problems. firebase use bizzy-book-dev firebase init firebase ...

Elements listed are often ignored by HTML

My website sends a URL and xpath query to the server in order to extract data from the URL based on the xpath query. While it is functioning properly, I am encountering a singular issue. When I specify an xpath query for href,text, it displays a list of a ...