简体   繁体   English

在扩展面板上选择收音机会更改另一个组件 vuetify 中的属性

[英]selecting radios on an expansion panel changes properties in another component vuetify

I have a component with expansion panels and radio buttons.我有一个带有扩展面板和单选按钮的组件。 and another component with bunch of pictures.和另一个带有一堆图片的组件。 My goal is to either show or not show the pictures based on what radio button is selected.我的目标是根据选择的单选按钮显示或不显示图片。

I have written a code but it doesn't work:我写了一个代码,但它不起作用:

This is the component with radio buttons(IFModule.vue):这是带有单选按钮的组件(IFModule.vue):

<template>
 .
 . 
  <app-ifpics></app-ifpics>
  <v-expansion-panel
  >
    <v-expansion-panel-content class="elevation-0"
                               v-for="(item,i) in stuff"
                               :key="i"
                               :label="item.label"
    >
      <div slot="header">{{item.label}}
        <v-radio-group height="0" row
                       :mandatory="false" v-model="item.subsYes"
                       @click.stop="showPicture(i, value)">


          <v-radio label="Yes" value="Yes" ></v-radio>
          <v-radio label="No"  value="No"></v-radio>
        </v-radio-group>
      </div>

      <v-card>
        <v-card-text>{{item.detail}}</v-card-text>
      </v-card>
    </v-expansion-panel-content>
  </v-expansion-panel>
 .
 .
</template>

<script>
  import IFPics from "./IFPic";
  import { ifBus } from "../../../main";

  export default {
    data(){
      return{
        stuff: [
          {label: 'Do you want this board', detail: 'Details: This is a very good board.', subsYes: 'input3', subsNo: 'input3No'},
      {label: 'Do you want this board', detail: 'Details: This is a very good board.', subsYes: 'input2', subsNo: 'input2No'},
      {label: 'Do you want this board', detail: 'Details: This is a very good board.', subsYes: 'input1', subsNo: 'input1No'},
      {label: 'Do you want this board', detail: 'Details: This is a very good board', subsYes: 'output7', subsNo: 'output7No'},
      {label: 'Do you want this board', detail: 'Details: This is a very good board', subsYes: 'output6', subsNo: 'output6No'},
      {label: 'Do you want this board', detail: 'Details: This is a very good board', subsYes: 'output5', subsNo: 'output5No'},
      {label: 'Do you want this board', detail: 'Details: This is a very good board', subsYes: 'output4', subsNo: 'output4No'},
      {label: 'Do you want this board', detail: 'Details: This is a very good board', subsYes: 'output3', subsNo: 'output3No'},
      {label: 'Do you want this board', detail: 'Details: This is a very good board', subsYes: 'output2', subsNo: 'output2No'},
      {label: 'Do you want this board', detail: 'Details: This is a very good board', subsYes: 'output1', subsNo: 'output1No'},
      {label: 'Do you want this board', detail: 'Details: This is a very good board', subsYes: 'power', subsNo: 'powerNo'}
        ]
      }
    },
   methods:{
      showPicture(index){
        const hasan = this.stuff[index].subsYes;
        const asghar = this.stuff[index].subsNo;
        ifBus.$emit('value', hasan);
        ifBus.$emit('item', asghar);
      }
    },
    components:{
      appIfpics: IFPics
   }
  }
</script>

And here is my component with pictures(IFPic.vue):这是我的图片组件(IFPic.vue):

<template>
  <div>
      .
      .
      <v-layout column>
        <div id="pic">
          <img src="../../../assets/Subrackpics/IF/EmptySub.png" height="236" width="500"/>
        </div>
        <img v-if="shoPower" src="../../../assets/Subrackpics/IF/power.png" height="232"  width="71" id="power" >
        <img v-if="showOutput1 " src="../../../assets/Subrackpics/IF/output1.png" height="233" width="46" id="output1">
        <img v-if="showOutput2" src="../../../assets/Subrackpics/IF/output2.png" height="233" width="47" id="output2">
        <img v-if="showOutput3"  src="../../../assets/Subrackpics/IF/output3.png" height="233" width="48" id="output3">
        <img v-if="showOutput4" src="../../../assets/Subrackpics/IF/output4.png" height="233" width="46" id="output4">
        <img v-if="showOutput5" src="../../../assets/Subrackpics/IF/output5.png" height="233" width="47" id="output5">
        <img v-if="showOutput6" src="../../../assets/Subrackpics/IF/output6.png" height="233" width="47" id="output6">
        <img v-if="showOutput7" src="../../../assets/Subrackpics/IF/output7.png" height="233" width="47" id="output7">
        <img v-if="showInput1" src="../../../assets/Subrackpics/IF/input1.png" height="233" width="37" id="input1">
        <img v-if="showInput2"  src="../../../assets/Subrackpics/IF/input2.png" height="233" width="37" id="input2">
        <img v-if="showInput3"  src="../../../assets/Subrackpics/IF/input3.png" height="233" width="37" id="input3">
      . 
      .
</template>

<script>
  import { ifBus } from "../../../main";

  export default {
    data() {
      return {
        item:'',
        value:'',
        shoPower:'',
        showOutput7:'',
        showOutput6:'',
        showOutput5:'',
        showOutput4:'',
        showOutput3:'',
        showOutput2:'',
        showOutput1:'',
        showInput3:'',
        showInput2:'',
        showInput1:'',
      }
    },
 created() {
  ifBus.$on('value', hasan => {
    this.value = hasan
  });
  ifBus.$on('item', asghar => {
    this.item = asghar
  });

  if (this.item === 'powerNo' && this.value === 'Yes') {
    this.shoPower = true;
  }
  else if (this.item === 'powerNo' && this.value === 'No') {
    this.shoPower = false;
  }

  if (this.item === 'output1No' && this.value === 'Yes') {
    this.showOutput1 = true;
  }
  else if (this.item === 'output1No' && this.value === 'No') {
    this.showOutput1 = false;
  }

  if (this.item === 'output2No' && this.value === 'Yes') {
    this.showOutput2 = true;
  }
  else if (this.item === 'output2No' && this.value === 'No') {
    this.showOutput2 = false;
  }

  if (this.item === 'output3No' && this.value === 'Yes') {
    this.showOutput3 = true;
  }
  else if (this.item === 'output3No' && this.value === 'No') {
    this.showOutput3 = false;
  }

  if (this.item === 'output4No' && this.value === 'Yes') {
    this.showOutput4 = true;
  }
  else if (this.item === 'output4No' && this.value === 'No') {
    this.showOutput4 = false;
  }

  if (this.item === 'output5No' && this.value === 'Yes') {
    this.showOutput5 = true;
  }
  else if (this.item === 'output5No' && this.value === 'No') {
    this.showOutput5 = false;
  }

  if (this.item === 'output6No' && this.value === 'Yes') {
    this.showOutput6 = true;
  }
  else if (this.item === 'output6No' && this.value === 'No') {
    this.showOutput6 = false;
  }

  if (this.item === 'output7No' && this.value === 'Yes') {
    this.showOutput7 = true;
  }
  else if (this.item === 'output7No' && this.value === 'No') {
    this.showOutput7 = false;
  }

  if (this.item === 'input1No' && this.value === 'Yes') {
    this.showInput1 = true;
  }
  else if (this.item === 'input1No' && this.value === 'No') {
    this.showInput1 = false;
  }

  if (this.item === 'input2No' && this.value === 'Yes') {
    this.showInput2 = true;
  }
  else if (this.item === 'input2No' && this.value === 'No') {
    this.showInput2 = false;
  }

  if (this.item === 'input3No' && this.value === 'Yes') {
    this.showInput3 = true;
  }
  else if (this.item === 'input3No' && this.value === 'No') {
    this.showInput3 = false;
  }
}

So again for example I want to select yes on the first group of radiobuttons and by doing so one of the pictures show and if I click no the picture disappears and so on.因此,再次举例来说,我想在第一组单选按钮上选择“是”,然后显示其中一张图片,如果我点击“否”,图片就会消失,依此类推。

What's the best way to do it?最好的方法是什么?

created method only run once when component is created, so your if else conditions are not called when ifBus receive event. created方法仅在创建组件时运行一次,因此当ifBus接收事件时不会调用您的 if else 条件。 You need to put it in event handler function.你需要把它放在事件处理函数中。 Here I extract all conditions to a separate method:在这里,我将所有条件提取到一个单独的方法中:

created() {
  ifBus.$on('value', hasan => {
    this.value = hasan
    this.changeDisplay()
  });
  ifBus.$on('item', asghar => {
    this.item = asghar
    this.changeDisplay()
  });
},
methods: {
  changeDisplay() {
    if (this.item === 'powerNo' && this.value === 'Yes') {
      this.shoPower = true;
    }
    else if (this.item === 'powerNo' && this.value === 'No') {
      this.shoPower = false;
    }

    if (this.item === 'output1No' && this.value === 'Yes') {
      this.showOutput1 = true;
    }
    else if (this.item === 'output1No' && this.value === 'No') {
      this.showOutput1 = false;
    }

    if (this.item === 'output2No' && this.value === 'Yes') {
      this.showOutput2 = true;
    }
    else if (this.item === 'output2No' && this.value === 'No') {
      this.showOutput2 = false;
    }

    if (this.item === 'output3No' && this.value === 'Yes') {
      this.showOutput3 = true;
    }
    else if (this.item === 'output3No' && this.value === 'No') {
      this.showOutput3 = false;
    }

    if (this.item === 'output4No' && this.value === 'Yes') {
      this.showOutput4 = true;
    }
    else if (this.item === 'output4No' && this.value === 'No') {
      this.showOutput4 = false;
    }

    if (this.item === 'output5No' && this.value === 'Yes') {
      this.showOutput5 = true;
    }
    else if (this.item === 'output5No' && this.value === 'No') {
      this.showOutput5 = false;
    }

    if (this.item === 'output6No' && this.value === 'Yes') {
      this.showOutput6 = true;
    }
    else if (this.item === 'output6No' && this.value === 'No') {
      this.showOutput6 = false;
    }

    if (this.item === 'output7No' && this.value === 'Yes') {
      this.showOutput7 = true;
    }
    else if (this.item === 'output7No' && this.value === 'No') {
      this.showOutput7 = false;
    }

    if (this.item === 'input1No' && this.value === 'Yes') {
      this.showInput1 = true;
    }
    else if (this.item === 'input1No' && this.value === 'No') {
      this.showInput1 = false;
    }

    if (this.item === 'input2No' && this.value === 'Yes') {
      this.showInput2 = true;
    }
    else if (this.item === 'input2No' && this.value === 'No') {
      this.showInput2 = false;
    }

    if (this.item === 'input3No' && this.value === 'Yes') {
      this.showInput3 = true;
    }
    else if (this.item === 'input3No' && this.value === 'No') {
      this.showInput3 = false;
    }
  }
}

By the way, you should try to avoid many if else condition like that.顺便说一句,你应该尽量避免很多这样的if else条件。 You should create mapping and check condition with for loop.您应该使用 for 循环创建映射和检查条件。

Reference to Vue component lifecycle diagram参考Vue组件生命周期图

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

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