vue+elementui selet框組件封裝 傳值


<template>
<el-select v-model="svalue" placeholder="請選擇" filterable clearable @change="handleButton">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
</template>
<!--調用寫法 @handleButton='handleButton' handleButton是方法名-->
<!--<xdh-select :dictType="'IT-BU-0002'" v-model="form.inter_options_value" @handleButton="handleButton"></xdh-select>-->

<script>
import axios from 'axios';
import {platform} from '@/api/address';

export default {
name: 'XdhSelect',
data() {
return {
options:[],
svalue: this.value
}
},
props: {
dictType: {
type:String
},//請求的碼表值
value: {
type: String
}//接受外部v-model傳入的值
},
methods: {
// 獲取下拉框數據
getDictionary_option(dictType){
this.options = JSON.parse(localStorage.getItem("dictList"))[dictType].obj;

/*let temp = [];
let that= this;
return axios({
method: 'get',
url: platform + 'platform/application/dict/common/findByDictType?dictType='+dictType
}).then(function (res) {
let error = ""
if(res.data.code == 0){
let tempData = res.data.result
for (let i in tempData){
let test = {};
test ={ value:i,label:tempData[i]}
temp.push(test)
}
}else{
error = res.data.msg
}
return new Promise((resolve, reject)=>{
if (error !== ''){
reject(error)
return
}
that.options = temp;
resolve(temp)
})
});*/
},
// 下拉框點擊事件
handleButton(){
/* 子組件通過事件發射向父組件傳遞事件及參數,$emit即為發射事件
第一個參數為向父組件傳遞的事件名,第二個參數為向父組件傳遞的參數 */
this.$emit( 'handleButton', this.svalue);
}
},
watch:{
//判斷下拉框的值是否有改變
value(val) {
this.svalue = val;//②監聽外部對props屬性result的變更,並同步到組件內的data屬性myResult中
},
svalue(val, oldVal){
if(val!=oldVal) {
this.$emit("input", val);//③組件內對myResult變更后向外部發送事件通知
}
}
},
mounted(){
// this.svalue=this.value;//初始話下拉框的值
this.getDictionary_option(this.dictType);
}
}
</script>


免責聲明!

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



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