在vuejs

时间:2017-06-13 12:38:20

标签: vue.js vuejs2

我正在寻找VueJS最佳实践,以便在以下代码段中同步输入元素和switch元素:

<div class="filter panel">
  <div class="field_title">Device</div>
  <el-switch v-model="switches.device_switch" name="device"></el-switch>
  <b-form-input v-model="device" placeholder="Device"></b-form-input>
</div> 

如果输入字段包含我想将v-model="switches.device_switch"设置为true的任何文字。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

为绑定到输入的device属性设置观察程序。在观察者中,您可以根据switches.device_switch字符串的长度设置device的值:

watch: {
  device: function(value) {
    this.switches.device_switch = value.length !== 0;
  }
}