admin管理员组文章数量:1404612
I have a basic question related to argument of a function.
when emitting an event from a child to a parent or when triggering a click event, I've seen in somme cases the add of $event as a parameter to a function and when removing it the code don't work as expected.
why is this $event needed ? and why in some cases is it used and in others not ? which is the difference ?
example where not used:
parent-component.vue
<div id="app">
<p>Count is: {{ count }}</p>
<HelloWorld @appendByOne="appendByOne()"/>
</div>
data() {
return {
count:0,
};
},
methods: {
appendByOne(){
this.count = this.count+1;
}
}
I have a basic question related to argument of a function.
when emitting an event from a child to a parent or when triggering a click event, I've seen in somme cases the add of $event as a parameter to a function and when removing it the code don't work as expected.
why is this $event needed ? and why in some cases is it used and in others not ? which is the difference ?
example where not used:
parent-component.vue
<div id="app">
<p>Count is: {{ count }}</p>
<HelloWorld @appendByOne="appendByOne()"/>
</div>
data() {
return {
count:0,
};
},
methods: {
appendByOne(){
this.count = this.count+1;
}
}
child-component.vue
<div>
<button @click="append()">Add 1</button>
</div>
methods: {
append(){
this.$emit('appendByOne');
}
}
example where is used:
<v-btn @click="saveText($event)"> apply </v-btn>
methods: {
saveText(event) {
//run this code
}
Share
Improve this question
asked Mar 10 at 16:51
ftouh yahamftouh yaham
53 bronze badges
1 Answer
Reset to default 0saveText
is not representative, @click="saveText($event)"
and @click="saveText"
are equivalent.
The explicit use of $event
is necessary when function signature is something different than 1 event argument, e.g. @click="saveTextForId($event, someId)"
. Also it can be used to avoid declaring a method, @click="$emit('eventName', $event)"
.
本文标签: vuejscases for using event as param of a functionStack Overflow
版权声明:本文标题:vue.js - cases for using $event as param of a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744834089a2627525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论