[英]Vue.js 3 call anonymous function inside event handler
Is it possible to call an anonymous function inside the event handler on Vue.js 3?是否可以在 Vue.js 3 上的事件处理程序内调用匿名 function?
I found these options across the internet:我在互联网上找到了这些选项:
<button @click="return function() { console.log('test')}()">Click me</button>
<button @click="() => { console.log('test') }">Click me</button> //ES6
<button @click="(function(){ console.log('Test'); })();">Click me</button>
None of them seems to be working.他们似乎都没有工作。 Maybe they used to work with Vue.js 2 but I can't confirm that.
也许他们曾经与 Vue.js 2 一起工作,但我无法确认。
The issue is that console
is not defined in the template, so you should define it as property in your data, or call a method that use the console
:问题是模板中没有定义
console
,因此您应该将其定义为数据中的属性,或者调用使用console
的方法:
<button @click="() => { logDatat('some data log') }">Click me</button>
<button @click="() => { myConsole.log }">Click me</button>
....
data(){
return {
myConsole : console
}
}
The three syntaxes are valid as shown in this demo:这三种语法是有效的,如本演示所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.