[英]Can I make it so a click event only executes if a certain variable is true?
I want to make it so that a user can only trigger an on click event and then execute a function only if a certain variable is true.我想让它使用户只能触发点击事件,然后仅当某个变量为真时才执行函数。 For example:例如:
<button @click='hello'>Trigger</button>
data: function() {
return {
isAuth: true
}
},
methods: {
hello(){
console.log('hello')
}
}
I just want to point out that I don't want to only hide the button.我只想指出,我不想只隐藏按钮。 I want to make clicks on the button element not trigger unless isAuth: true.我想点击按钮元素不会触发,除非 isAuth: true。
I guess you can do the following way, if you really want:我想你可以按照以下方式进行,如果你真的想要:
<button @click='isAuth ? hello : {}'>Trigger</button>
but honestly, this, to me, is not the right way.但老实说,对我来说,这不是正确的方法。 and I think you should consider calling the function on button click and inside that function, you can use an if statement:我认为您应该考虑在单击按钮时调用该函数,并且在该函数内部,您可以使用 if 语句:
<button @click='hello'>Trigger</button>
data: function() {
return {
isAuth: true
}
},
methods: {
hello(){
if (!this.isAuth) return;
console.log('hello')
}
}
<button @click='hello'>Trigger</button>
data: function() {
return {
isAuth: true
}
},
methods: {
hello() {
if (this.isAuth) {
console.log('hello')
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.