简体   繁体   English

Vuejs:如何在 V 模型中不使用 boolean 值?

[英]Vuejs : How to not a boolean value in V-model?

I am getting value of options.customerdata.showbutton as true from an API, so now the switch is in on condition.我从 API 中将options.customerdata.showbutton的值设为true ,所以现在开关处于开启状态。

I want the switch to be in off, hence v-model="false" should be given.我希望开关处于关闭状态,因此应该给出v-model="false" But tried giving但尝试给予

v-model="!(options.customerdata.showbutton)"

does not work and shows error.不起作用并显示错误。 How to achieve this?如何做到这一点?

<b-form-checkbox v-model="options.customerdata.showbutton" name="logo-display" switch >
</b-form-checkbox>

Model can't be expression (it must be reference to data/property) Model 不能是表达式(它必须是对数据/属性的引用)

Easiest way is define data and set it with negated value.最简单的方法是定义数据并将其设置为否定值。 But be aware that in this case your model is not change when customerdata is changed after component initialization.但请注意,在这种情况下,您的 model 在组件初始化后更改客户数据时不会更改。

data: {
  return {
    show: !this.options.customerdata.showbutton
  } 
}

If you need to store value back to options (or bound value to customerdata), you can use also computed property with setter/getter如果您需要将值存储回选项(或将值绑定到客户数据),您还可以将计算属性与 setter/getter 一起使用

computed {
  show: {
    get () {
      !this.options.customerdata.showbutton
    }

    set (value) {
      this.options.customerdata.showbutton = !value
    }
  }
}

For both case your bind it with对于这两种情况,您都将其绑定

v-model="show"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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