template的代码:
<el-form-item label="设备类型:" prop="equipmentTypeDictionaryId">
<el-select v-model="equipmentTypeInfo.equipmentTypeDictionaryId" filterable placeholder="试试搜索:访客机" @change="selectType">
<el-option v-for="item in deviceType" :key="item.equipmentTypeDictionaryId" :label="item.equipmentTypeName" :value="item.equipmentTypeDictionaryId" />
</el-select>
</el-form-item>
script中method里代码:
// 选择设备类型
selectType (id) {
let selectedItem = {}
selectedItem = this.deviceType.find((item) => { // 这里的deviceType就是上面遍历的数据源
return item.equipmentTypeDictionaryId === id // 筛选出匹配数据,是对应数据的整个对象
})
this.equipmentTypeInfo.typeName = selectedItem.equipmentTypeName // 设备类型
},
案例中是通过equippiypeDictionaryId 匹配找到其selectedItem,从而实现获取selectedItem.equipmentTypeName