在作後臺管理時,使用了vue搭配elementUI,請求方法使用了axios插件,在使用下拉框時,由於我須要獲取選中的選項中的其餘值,所以須要傳入對象。對select下拉框的文檔沒有讀的很仔細,百度過幾篇文章,也沒有理解他們表達的意思,而後本身又去看文檔,把他的屬性看了幾遍,突然就來了靈感,嘗試了一兩次,哇,原來是這樣作,爽歪歪,真的是書讀百遍其義自見
<template> <el-select v-model="seletedOption" value-key="name" style="width: 72%" placeholder="請選擇"> <el-option v-for="item in options" :key="item.express" :value="item" :label="item.name" ></el-option> </el-select> </template> <script> export default{ data() { return { selectedOption: '', options: [ { id: 0, name: '11', title: 'one' }, { id: 1, name: '22', title: 'two' } ] } } } </script>
這個value-key指的是對象中你要渲染或者說是你要選中的鍵值,是直接寫死的,好比:我這裏要渲染要選中的就是name對應的值,在我沒有設置value-key這個屬性可是卻直接傳入el-option中的value爲對象時,發現即便selectedOption爲空,他在頁面上也有顯示值vue
.catch(err => { console.log(err); console.log(err.response); })
/* 最外層 */ .customModal{ position: fixed; overflow-y: scroll; width: 100%; height: 100vh; left: 0; top: 0; bottom: 0; right: 0; background: rgba(0, 0, 0, .6); z-index: 1000; } /* 內容層 */ .modal{ position: relative; margin: 15vh auto 50px; width: 600px; min-height: 242px; background: #fff; border-radius: 3px; }
/* new Date()獲取的時間戳是以毫秒爲單位,我這裏後臺返回的是以秒爲單位 */ let time = Math.floor(new Date() / 1000);
import { provinceAndCityData, regionData, provinceAndCityDataPlus, regionDataPlus, CodeToText, TextToCode } from 'element-china-area-data'
provinceAndCityData是省市二級聯動數據(不帶「所有」選項)
regionData是省市區三級聯動數據(不帶「所有」選項)
provinceAndCityDataPlus是省市區三級聯動數據(帶「所有」選項)
regionDataPlus是省市區三級聯動數據(帶「所有」選項)
"所有"選項綁定的value是空字符串""
CodeToText是個大對象,屬性是區域碼,屬性值是漢字 用法例如:CodeToText['110000']輸出北京市
TextToCode是個大對象,屬性是漢字,屬性值是區域碼 用法例如:TextToCode['北京市'].code輸出110000,TextToCode'北京市'.code輸出110100,TextToCode'北京市'['朝陽區'].code輸出110105
<template> <el-cascader style="width: 68%" size="large" :options="options" v-model="selectedCity" placeholder="請選擇區域" ></el-cascader> </template> <script> import { provinceAndCityData,CodeToText } from 'element-china-area-data' export default{ data() { return { options: provinceAndCityData, selectedCity: [], } }, method: { handleSelected() { /* 這裏他返回的是選中的編號,須要進行編號轉文字 */ console.log(CodeToText[this.selectedCity[0]]); } } } </script>
正在努力學習中,若對你的學習有幫助,留下你的印記唄(點個贊咯^_^)