简体   繁体   English

我可以让它只在某个变量为真时才执行点击事件吗?

[英]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.

相关问题 我如何才能使此脚本仅在特定屏幕尺寸以上执行? - How can I make it so this script executes only above a certain screen size? 如何仅针对某些元素覆盖 javascript 单击事件? - How can I override javascript click event for only certain elements? 如何确保我的click函数只用Jquery执行一次? - How can I make sure that my click function executes one time only with Jquery? 如何使执行事件的功能单击jQuery - how to make a function that executes an event click on jquery 分页单击事件仅执行一次 - Pagination click event executes only once 如何确保在所有其他附加的单击处理程序之后执行特定的单击事件处理程序? - How can I ensure a particular click event handler executes after all other attached click handlers? 如何制作一个按钮,使其只能在一定程度上被拖动? - How to make a button so that it can only be dragged to a certain extent? 如何仅在第一次条件为真时才保存变量 - How can I make variable save only the first time the condition is true 我可以引用变量的点击事件吗? - Can I reference a click event on a variable? React.js 中的点击处理程序在事件发生之前执行。 我怎么能阻止呢? - The on click handler in React.js executes before the event happens. How can I stop that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM