elementUI el-select獲取點擊項的整個對象item


在vue2引入elementUI之后,經常會遇到此類需求,el-select獲取點擊項的整個對象item,而不是默認的v-model 項目

官方文檔有 value-key="value" 的用法 
https://element.eleme.cn/#/zh-CN/component/select#select-attributes

 案例

<template>
  <div>
  <el-select v-model="value" value-key="value" @change="change" placeholder="請選擇">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="`${item.label}`"
      :value="item"><!--綁定整個對象item-->
      {{item.label}}
    </el-option>
  </el-select>
  </div>
</template>


<script>
export default {
  name: 'HelloWorld',
  data (){
    return {
    options: [{
        value: '選項1',
        label: '黃金糕'
      }, {
        value: '選項2',
        label: '雙皮奶'
      }, {
        value: '選項3',
        label: '蚵仔煎'
      }, {
        value: '選項4',
        label: '龍須面'
      }, {
        value: '選項5',
        label: '北京烤鴨'
      }],
      value: ''
    }
  },
  created() {

    // 初始化賦值直接 賦值整個對象
    this.value = {
        value: '選項5',
        label: '北京烤鴨'
      };

  },
  methods: {

    // chnage觸發
    change(e) {
      console.log(e);// 打印整個對象 
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM