elementUI文档里有对应的属性 value-key
第一种方式:
xml
<template>
<!--v-model绑定一个对象值,指定value-key标识-->
<el-select v-model="obj" value-key="id" @change="change" placeholder="请选择">
<el-option
v-for="(item,index) in options"
:key="item.id"
:label="item.name"
<!--绑定整个对象item-->
:value="item">
{{ item.name }}
</el-option>
</el-select>
</template>
<script>
export default {
name: "test",
data() {
return {
options: [{
id: 1,
name: '黄金糕'
}],
obj: {}
}
},
methods: {
change(item) {
//item是整个对象
console.log(item);
}
}
}
</script>
第二种方式:
xml
<template>
<el-select v-model="formValue.id" value-key="id" @change="change" placeholder="请选择"> <el-option
v-for="(item,index) in options"
:key="item.id"
:label="item.name"
:value="item.id">
{{ item.name }}
</el-option>
</el-select>
</template>
<script>
export default {
name: "test",
data() {
return {
options: [{
id: 1,
name: '黄金糕'
}],
}
},
methods: {
change(id) {
//根据id获取对应name
this.formValue.name= this.options.find((item) => item.id == id ).name;
}
}
}
</script>