方式一,在<el-select>中添加 ref="mySelected",如
<el-select v-model="currentTool" filterable placeholder="请选择" ref="mySelected"></el-select>
在取值的时候就可以通过以下方式取值
this.$refs.mySelected.selected.value
this.$refs.mySelected.selected.label
方式二,在<el-option>中添加 ref="mySelected",如
<el-option v-for="(item,index) in tools" :key="index" :label="item.name" :value="item.id"></el-option>
在取值的时候就可以通过以下方式取值
this.$refs.mySelected[0].select.selected.label
this.$refs.mySelected[0].select.selected.value
解释:如果添加在<el-option>,在控制台打印时会看到this.$refs.mySelected是一个数组,但这数组里的select.selected这一段的值是一样的,所以取第0个就行
文章介绍了在Vue.js中,如何通过ref属性从<el-select>组件和<el-option>组件获取选中值。方法一是给<el-select>添加ref,然后通过this.$refs.mySelected.selected获取值;方法二是将ref添加到<el-option>,尽管此时this.$refs.mySelected是数组,但可以通过[0].select.selected获取相同值。
2493

被折叠的 条评论
为什么被折叠?



