Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bound event with once gets reused in v-if/else condition #8032

Closed
ghost opened this issue Apr 16, 2018 · 5 comments
Closed

bound event with once gets reused in v-if/else condition #8032

ghost opened this issue Apr 16, 2018 · 5 comments

Comments

@ghost
Copy link

ghost commented Apr 16, 2018

Version

2.5.16

Reproduction link

https://codepen.io/katedo17/pen/rdXMbp

Steps to reproduce

Enter "3" in the input field, and click the link "Test Btn 2".

What is expected?

It should only trigger the on click event that was configured for the "Test Btn 2".

What is actually happening?

It triggers the function binded to "Test Btn 1" and then trigger the "Test Btn 2".


This only happens when I use v-if in the parent element of the link. But if I use v-show, it doesn't have the issue.

@posva posva changed the title v-on:click.once doesn't work with v-if bound event with once gets reused in v-if/else condition Apr 16, 2018
@posva
Copy link
Member

posva commented Apr 16, 2018

As a note, you should not use both, v-on and onclick (Vue cannot detect the later)
You can workaround this by adding a key to one or both of the a elements

@posva posva added the bug label Apr 16, 2018
@ghost
Copy link
Author

ghost commented Apr 16, 2018

Noted. The onclick attribute is just to test the issue. My use case is, the link with v-on:click.once is supposed to call to the server, while the other one supposed to redirect to another page with no JS involved. But I was having an issue because when I click the link to redirect, it is actually doing a call to the server.

Thanks for the help. It works by adding "key" attribute.

@javoski
Copy link
Member

javoski commented Apr 16, 2018

If you want to prevent the default action of an event, please use .prevent modifier.

@gebilaoxiong
Copy link
Member

gebilaoxiong commented Apr 16, 2018

It seems that once listener was not properly destroyed

@cobain46
Copy link

I'm not a English native speaker, I'll do my best to describe it. The reason for this problem is element reuse,
The existing elements are usually reused for the efficiency of rendering. So if you want make the function like you want , you need to add a key to the 'input'. key must be unique value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants