How to control these components?

人盡茶涼 提交于 2019-12-04 18:51:25

Is this what you wanted? codepen (or perhaps cleaner: codepen)

<v-switch v-for="(person, i) in people" 
  :key="person.label"
  v-model="people[i].value"
  :label="people[i].label"
  :disabled="people[i].disabled"
></v-switch>

people: [
  { value: false, label: "A", disabled: false },
//...

created() {
// here we watch for D and E changes, and set A to true if both D and E are ON
this.$watch(
  vm => vm.people[3].value && vm.people[4].value, (newVal, oldVal) => {
    if(newVal) {
      this.people[0].value = true;
    }         
  });
},
watch: {
  "people.0.value"(newVal) {
    if (newVal) { // if A is turned ON
      // turn D and E on, and disable them
      this.people[3].value = true;
      this.people[4].value = true;
      this.people[3].disabled = true;
      this.people[4].disabled = true;
    } else { // if A is turned OFF
      // enable D and E
      this.people[3].disabled = false;
      this.people[4].disabled = false;
    }
  }
}

Not sure what else to add. Basically watch for changes and react on them.

Using computed getters/setters provides a method of backing view properties with logic. Rather than directly linking to data, Vue will call the computed methods with whatever logic has been supplied.

The second part of the problem is disabling buttons. This can be accomplished with the v-switch disabled property:

<v-switch
  :disabled="computedPropertyToDisable"
  v-model="computedPropertyForValue"
  ...
</v-switch>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!