【发布时间】:2021-07-02 23:27:48
【问题描述】:
我在vue中做了一个switch组件。 并且这个 switch 组件将选择的数据发送到它的父模板,如下所示。
switchform.vue
模板
<div :class = "{ 'switch-field' : !smallSelection,
'small-switch-field':smallSelection}"
v-if='twoSelection'>
<input type="radio" id="radio-first" name="switch-first"
@change="categoryChange"
:value="options[0]"
v-model='selected'
/>
<label for="radio-first">{{options[0].toUpperCase()}}</label>
<input type="radio" id="radio-second" name="switch-second"
@change="categoryChange"
:value="options[1]"
v-model='selected'
/>
<label for="radio-second">{{options[1].toUpperCase()}}</label>
</div>
方法
methods:{
categoryChange(){
this.$emit("selectedCategory", this.selected);
}
},
但是,当我在一个视图中使用此组件两次时, 两个不同的按钮 ui 组件传达相同的数据。
父母
<div class='main-body'>
<div class='ticket-count-analysis'>
<div class='analysis-title'>
<div style='flex-grow:1'>Type of tickets</div>
<div class='hr-line white'></div>
</div>
<div>{{currentRatioCategory}}</div>
<switch-form
:options='options'
:smallSelection=false
@selectedCategory='changeTypeCategory'
class='option-switch'
></switch-form>
</div>
<div class='ticket-ratio-analysis'>
<div class='analysis-title'>
<div style='flex-grow:1'>Proportion of ticket by type</div>
<div class='hr-line white'></div>
</div>
<div>{{currentTypeCategory}}</div>
<switch-form
:options='options'
:smallSelection=false
@selectedCategory='changeRatioCategory'
class='option-switch'
></switch-form>
</div>
</div>
如何让两个不同的 '' 不共享父级中的数据。 我想多次使用该组件,并且我希望每个都发出自己的数据,以便我可以使用两个不同的数据。
左侧将使用来自左侧开关的数据 而右侧将使用从右侧开关接收到的数据。
【问题讨论】: