Currently, I am learning Vue and encountered a problem that I need help with.
When using the v-on:click directive to call a method, all other instance methods are also called when the same method is used elsewhere.
HTML:
<div id="exercise">
<div><button class="test" v-on:click="newCheck()">New Challenge</button>
<p >Success check is: {{ passCheck }}</p>
</div>
<div>
<button class="roll" v-on:click="roll20()">Roll!</button>
<p>You Rolled a {{ roll20() }}</p>
</div>
</div>
JS:
new Vue({
el: '#exercise',
data: {
yourRoll: '10',
passCheck: '10',
},
methods: {
roll20: function(){
this.yourRoll = Math.round(Math.random()*19)+1;
return this.yourRoll;
},
newCheck: function(){
this.passCheck = Math.round(Math.random()*19)+1;
}
}
});
If {{ roll20() }} is used in the second paragraph, clicking the 'New Challenge' button triggers both roll20() and newCheck(). However, if {{ yourRoll }} is used instead, this doesn't happen.
Regardless, clicking 'Roll!' only executes the roll20() method.
Could someone please explain what exactly is happening here?
You can find a codepen illustrating the issue here: Codepen of code
Note: I managed to work around this problem by adopting a different approach, but I am still curious as to why this behavior occurred: Working Approach