[Vuejs]-Vue.js – how to properly conditionally set click handler code

3👍

Please don’t do that ! Vue wants you to separate state from code from markup. If you change the event listener conditionally, you’ve got a little bit of state living in the markup. @click should just point to a method, and all the conditional logic should be in the method. Just use if() inside your handler in the methods block to differentiate the two cases. It will be much easier to follow.

The thing about computed is that you don’t know/shouldn’t care when it runs. I suppose you could get into returning a function from a computed, using bind() to link the arguments, but I recoil in horror. I can’t see why it has to be so complex.

Leave a comment