Issue with registering @click event callback in Vue 2 when using conditional rendering

I've encountered an issue when trying to register an event callback on an SVG button while conditionally rendering it. For some reason, the @click event callback is not being registered, even if the condition is initially true. Interestingly, everything works fine once I remove the v-if directive. Is this behavior normal? Are there any workarounds for this?

Note: It seems to work fine without using the showControls variable and simply supplying v-if=true...

Here’s the code snippet of my component:

<template>
  <div id="svg-container" class="svg-container">

    <svg id="svg-canvas-outer" viewBox="0 0 1000 1000">
      <svg id="svg-canvas-inner" :viewBox="viewBox">
        <slot id="svg-content"></slot>
      </svg>

      <g transform="translate(30,30)" v-if="showControls"> 
        <g class="svg-button btn btn-primary" @click="zoomIn">
          <circle class="button" cx="0" cy="0" r="20"/>
          <path d="M 0 10 H 10 V 0 H 20 V 10 H 30 V 20 H 20 V 30 H 10 V 20 H 0 Z" fill="white"
                transform="translate(-15,-15)"/>
        </g>
        <g class="svg-button" transform="translate(-25, 25)" @click="resetView">
          <rect fill="black" height="30" width="50"/>
          <text dominant-baseline="middle" fill="white" text-anchor="middle" transform="translate(25, 15)">RESET</text>
        </g>
        <g class="svg-button" transform="translate(0, 80)" @click="zoomOut">
          <circle class="button" cx="0" cy="0" fill="black" r="20"/>
          <path d="M0 10 H 30 V20 H0 Z" fill="white" transform="translate(-15,-15)"/>
        </g>
      </g>

    </svg>
  </div>
</template>

<script>
  export default {
    name: 'SvgContainer',
    props: ['width', 'height'],
    data() {
      return {
        showControls: true,
        controlsWidthBase: 60,
        controlsHeightBase: 120,
        viewBoxWidth: this.width,
        viewBoxHeight: this.height,
        viewBoxScale: 1.0,
        viewBoxX: 0,
        viewBoxY: 0,
        startPoint: null,
        endPanX: 0,
        endPanY: 0,
        currentlyPanning: false,
        svgElement: undefined,
        mousePos: '',
      };
    },
    computed: {
      viewBox() {
        return `${this.viewBoxX} ${this.viewBoxY} ${this.scaledViewBoxWidth} ${this.scaledViewBoxHeight}`;
      },
      // Other computed properties...
      
    },
    methods: {
      // Various methods...
      
    },
    mounted() {
      this.bindEvents();
    },
  };
</script>

<style scoped>

</style>

Answer №1

Your element will be hidden when the mouseup event is triggered.

Please delete this line:

//container.addEventListener("mouseup", this.onMouseLeave);

Check out a live example here: Explore Vue SFC Demo

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

Authentication with Laravel and Vue.js

After successfully implementing Laravel-Vue.js Authentication using Passport API, I am now able to obtain the token and send requests to api/user. Initially, I used the normal authentication process by sending data through a POST request to /login, which r ...

Smoothly cycle material colors in separate THREE.js geometries

Hello everyone! Check out the codepen I provided for a multiple curves animation. My goal is to achieve a smooth hue change at every drawn curve. Each subsequent curve should have a slightly shifted hue, and I want to be able to control the duration of th ...

Is it feasible to style individual letters in a word within an input field?

Is it possible to change the styling of individual letters in an input containing text? For example, if the word 'Test' is in the input, can I make the 'Te' bold while leaving the 'st' regular? Alternatively, perhaps I'd ...

unplug the client from socket.io ID

Is it possible to have a client leave a room using just the socket ID with the io object? io.sockets(specificSocketID).leave('sampleroom'); The documentation mentions using the leave method with the socket, but I am looking for a way to achieve ...

What's the best way to transfer information from a spreadsheet to an HTML file using Google Apps Script?

I am attempting to transfer data from my current SpreadSheet to an HTML file created with Google App Script. Any guidance on this would be greatly appreciated as I am feeling a bit lost. Html <body> <!--3.x snippet--> <script src=" ...

Silence from PHP script after executing jQuery post() method

I've run into an issue while trying to retrieve data from a PHP file that reads a mySQL database. The data is delivered successfully when accessed through a browser: However, when attempting to access the data using jQuery's get() or post() meth ...

"Implementing drag and drop functionality in Vue.js without the need for a

Exploring html 5 drag and drop functionality using vue js has been a challenging experience for me. After following the w3schools tutorial on drag and drop, I managed to get it working in a basic html file but faced difficulties when implementing it in my ...

Utilizing DOMStringMap data to populate CSS URLs

I am attempting to create a universal CSS modification for a webpage element that resembles: <button data-action="link" class="cardImageContainer coveredImage cardContent itemAction lazy lazy-image-fadein-fast" data-blurhash="lo ...

Updating JavaScript files generated from TypeScript in IntelliJ; encountering issues with js not being refreshed

Struggling with a puzzling issue in IntelliJ related to the automatic deployment of changes while my server is running (specifically Spring Boot). I've made sure to enable the "Build project automatically" option in my IntelliJ settings. Whenever I ...

JavaScript Evaluation Error

try { eval(somejavascript); } catch(e) { console.log(e); } When I encounter runtime errors like: TypeError: Cannot call method 'leftPad' of undefined I wonder if there is any way to debug this error. Specifically, I'm looking for ...

Vue application experiences issues with styles not displaying as expected

I've recently put together my very first Vue web application. As I was building it, I ran into an issue with my delete buttons not displaying correctly. No matter what styling adjustments I made, the problem persisted. Any suggestions on how to fix th ...

Data is not being refreshed by Ajax

In my forum, users are only allowed to edit and delete their own comments. I have set up an "edit" button that opens a modal when clicked, giving the user access to the data they submitted before. I've written an ajax function to target these fields a ...

Is it necessary for me to indicate that I need the password from results[0] when the MySQL query only returns one result?

When querying the database and receiving only one row/result, make sure to reference the password column correctly. Use this code to ensure you are getting the correct value: var hash = results[0].password; If you try to use var hash = results.password; ...

Update the displayed number on the input field as a German-formatted value whenever there is a change in the input, all while maintaining

In German decimal numbers, a decimal comma is used. When formatting, periods are also used for grouping digits. For example, the number 10000.45 would be formatted as 10.000,45. I am looking to create an input field (numeric or text) where users can input ...

Encountering a buffer error when utilizing child processes in Express

I'm currently working on a project where a user can upload a document and a Python script located on the server side in a MERN stack application will be executed using child processes to determine if it returns true or false. However, when I try to op ...

The v-focus directive in Vue.js is causing issues with typing in one input field, while the other input fields are not functioning correctly

Whenever I try to focus on a specific input field upon clicking a button, that input field remains focused and I am unable to enter data into the second input field. Even if I try to insert a word on keyup, it still stays focused on the first field. <d ...

What is the best way to extract information from a .txt file and transform it into an integer format?

I am currently working on a project involving a distance sensor that retrieves data from a Raspberry Pi and displays it on a website. The goal is to have the data stored in a .txt file and then transferred to the website for display. I am seeking assistanc ...

Node.js: Error - Module 'html' not found

Currently, I am utilizing Node.js and attempting to exclusively serve HTML files without any Jade or EJS engines. Below is the code for my entry point (index.js): var express = require('express'); var bodyParser = require('body-parser&apo ...

Display map.svg on the browser and execute a function when any area is clicked

Creating an online parking system that is compatible with desktop, iPad, and Android devices is my current project. I have a .svg formatted map for the parking area. I am looking for advice on how to display this map in a browser and execute JavaScript fu ...

Choosing a combination of classes

For my web application, I created checkboxes that control the visibility of windows by toggling classes on elements. My JavaScript code successfully achieves this functionality. $(document).ready(function(){ $('#field').change(function(){ ...