Vuejs - 如果文本区域不为空,则更改布尔值

Vuejs - change boolean if textarea not empty

我正在尝试一些我认为非常简单的事情:

<textarea name="ask" class="form-control" v-model="text"></textarea>

还有 vue :

    data: {
      showLabel: true,
      text: ''
    },
    methods: {
      textareaValue(){
        return this.text
        if(this.text != '') {
          this.showLabel = false
        }
      }
    }

我可以在控制台中看到 'text' 数据值发生变化,但 showLabel 布尔神经元按照 test() 方法中的要求变为 false。

非常欢迎任何建议。

谢谢!

朱利安

你应该检查 computed properties。它适合您的用例。

您应该将 showLabel 声明为计算 属性 而不是在您的数据中:

computed: {
  showLabel () {
     return !this.text
  }
}