繁体   English   中英

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

[英]Can I make it so a click event only executes if a certain variable is true?

我想让它使用户只能触发点击事件,然后仅当某个变量为真时才执行函数。 例如:

<button @click='hello'>Trigger</button>

data: function() {
    return {
        isAuth: true
    }
},
methods: {
    hello(){
         console.log('hello')
    }
}

我只想指出,我不想只隐藏按钮。 我想点击按钮元素不会触发,除非 isAuth: true。

我想你可以按照以下方式进行,如果你真的想要:

<button @click='isAuth ? hello : {}'>Trigger</button>

但老实说,对我来说,这不是正确的方法。 我认为您应该考虑在单击按钮时调用该函数,并且在该函数内部,您可以使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM