簡體   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