[英]vuejs this.$refs for more than one element
我有4个选择:
<div class="form-row">
<div class="form-group col-3">
<label for="stateSelect">Status</label>
<select v-model="filterState" @change="search()" data-live-search="true" class="form-control" ref="stateSelect" id="stateSelect">
<option value="">Alle Status</option>
<option v-for="assignmentState in assignmentStates" v-bind:value="assignmentState.state">
{{ assignmentState.state }}
</option>
</select>
</div>
<div class="form-group col-3">
<label for="loadingSelect">Beladungsort</label>
<select v-model="filterLoading" @change="search()" data-live-search="true" class="form-control" ref="loadingSelect" id="loadingSelect">
<option value="">Alle Standorte</option>
<option v-for="location in locations" v-bind:value="location.id">
{{ location.name }}
</option>
</select>
</div>
<div class="form-group col-3">
<label for="unloadingSelect">Entladungsort</label>
<select v-model="filterUnloading" @change="search()" data-live-search="true" class="form-control" ref="unloadingSelect" id="unloadingSelect">
<option value="">Alle Standorte</option>
<option v-for="location in locations" v-bind:value="location.id">
{{ location.name }}
</option>
</select>
</div>
<div class="form-group col-3">
<label for="carrierSelect">Spediteur</label>
<select v-model="filterCarrier" @change="search()" data-live-search="true" class="form-control" ref="carrierSelect" id="carrierSelect">
<option value="">Alle Speditionen</option>
<option v-for="assignmentCarrier in assignmentCarriers" v-bind:value="assignmentCarrier.name">
{{ assignmentCarrier.name }}
</option>
</select>
</div>
</div>
我必须使用选择器 function 刷新这个 dom 元素,才能在 vuejs 中像这样进行引导选择工作:
export default {
name: "AssignmentList",
data() { ....
},
updated() {
$(this.$refs.stateSelect).selectpicker('refresh');
$(this.$refs.loadingSelect).selectpicker('refresh')
$(this.$refs.unloadingSelect).selectpicker('refresh')
$(this.$refs.carrierSelect).selectpicker('refresh')
}
是否可以总结这 4 个.selectpicker('refresh')
语句?
或者是否可以对这些选择进行分组/标记,以便我可以一次刷新这些元素而无需单独键入每个元素。
只需在constants.js
文件或其他文件中创建一个常量,然后将它们import
到您的组件中。 像这样的东西:
const refTypes = Object.freeze({
stateSelect: "stateSelect",
loadingSelect: "loadingSelect",
unloadingSelect: "unloadingSelect",
carrierSelect: "carrierSelect"
});
然后在您的组件中有一个您调用的方法,该方法执行以下操作:
Object.values(refTypes).forEach((type) => {
$(this.$refs[type].selectpicker('refresh');
});
我可能也会为'refresh'
做同样的事情
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.