Vue封裝遠(yuǎn)程下拉框組件的實(shí)現(xiàn)示例
之前封裝了一個(gè)遠(yuǎn)程搜索的輸入框,靜態(tài)在Vue官網(wǎng)看到一個(gè)類似的遠(yuǎn)程搜索下拉框,今天也封裝一個(gè)遠(yuǎn)程搜索下拉框,面對不同的需求

我們修改了官方提供的代碼來封裝了
父組件
RemoteSearch.vue
<template>
<el-row>
<el-select
v-if="chooseFlag ==0"
v-model="selectKey"
:multiple="false"
:filterable="true"
:remote="true"
@focus="selectFocus"
:clearable="true"
placeholder="請輸入內(nèi)容"
:remote-method="remoteMethod"
:loading="selectLoading">
<el-option
v-for="index in options"
:key="index[key]"
:label="index[labelValue]"
:value="index[key]">
</el-option>
</el-select>
<br>
<br>
<el-button @click="open" type="primary">點(diǎn)擊查看key,value</el-button>
</el-row>
</template>
<script>
export default {
name: "RemoteSearch",
data() {
return {
options: [], //存儲下拉框的數(shù)據(jù)
selectKey: "", //綁定的所選擇的key
selectEnterpriseForm: {},//發(fā)送數(shù)據(jù)
selectLoading: false,
}
},
props: {
chooseFlag: {
value: Number,
default: 0,
},
labelValue: {
type: String,
default: "name",
},
key: {
value: String,//key
default: "id",
},
RequestUrl: { //獲取數(shù)據(jù)的請求地址
value: String,
default: "/v1/teachers/findcourseNameByName",
},
},
mounted() {
console.log("mounted")
},
methods: {
refreshData(){
this.selectKey =""
},
selectEnterprise: function (query) { //query用戶搜索的值
this.selectEnterpriseForm = this.$options.data().selectEnterpriseForm; //清空數(shù)據(jù)
this.selectEnterpriseForm.labelValue = query;
this.axios({
method: "POST",
url: this.RequestUrl,
data: this.$data.selectEnterpriseForm,
}).then((res) => {
let code = res.data.code;
if (code == 200) {
this.options = [];
this.selectLoading = false;
// this.addLoading = false;
for (let i = 0; i < res.data.data.length; i++) {
this.options.push({[this.labelValue]: res.data.data[i][this.labelValue], [this.key]: res.data.data[i][this.key]});
}
}
}).catch((error) => {
console.log(error);
});
},
remoteMethod(query) {
this.selectLoading = true;
this.selectEnterprise(query);
},
selectFocus: function () {
this.options = [];
this.selectLoading = true;
this.selectEnterprise("");
},
open: function () {
alert("所選id為:" + this.selectKey)
}
}
}
</script>
<style scoped>
</style>
vue的參數(shù)是可以通過封裝在props內(nèi),被其他界面引用
注意:
一:js中在調(diào)用json格式數(shù)組的值的時(shí)候——有兩種形式
以下為dataList數(shù)組

形式一:dataList[0].name
形式二:dataList[0][name]
在有些時(shí)候會把**.變量**識別成調(diào)用,所以在一些情況下使用第二個(gè)效果更好

js的數(shù)組手動設(shè)置值(給dataList設(shè)置一個(gè)value值)
dataList.value = ?
以下為引用的vue界面
<template>
<div>
<RemoteSearch :choose-flag="0" :auto-complete-column="name" ref="refreshData"></RemoteSearch>
<el-button type="primary" @click="refreshChartSearch" style="margin-left: 10px">重置</el-button>
</div>
</template>
<script>
import RemoteSearch from "@/components/select/RemoteSearch";
export default {
components: {
RemoteSearch
},
data(){
return {
}
},
methods:{
refreshChartSearch(){
this.$nextTick(() => {
this.$refs.refreshData.refreshData();
//DOM渲染完畢后就能正常獲取了
})
},
},
}
</script>
<style scoped>
</style>
只需要通過import導(dǎo)入對應(yīng)的組件,通過components來調(diào)用,并通過類似標(biāo)簽的形式來聲明
子組件通過父組件提供的props的參數(shù)重寫(修改)父組件的參數(shù)
如果子組件不調(diào)用,props的參數(shù)就會是默認(rèn)的值。
子組件可以通過在標(biāo)簽內(nèi)使用:特定值的方式來修改值
重置的按鈕實(shí)現(xiàn),可以參考之前封裝遠(yuǎn)程搜索輸入框的帖子


這里父組件的placeholder也可以做成讓子組件自己選擇的,但是我這里的形式比較通用,就沒有修改,有興趣的可以自行優(yōu)化
placeholder="請輸入內(nèi)容"
到此這篇關(guān)于Vue封裝遠(yuǎn)程下拉框組件的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Vue封裝遠(yuǎn)程下拉框 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uniapp-vue3-彈出選擇組件wo-pop-selector使用示例
wo-pop-selector彈出選擇組件采用uniapp-vue3實(shí)現(xiàn), 支持H5、微信小程序,本文給大家介紹uniapp-vue3-彈出選擇組件wo-pop-selector及使用示例,感興趣的朋友一起看看吧2023-10-10
element中el-table與el-form同用并校驗(yàn)
本文主要介紹了element中el-table與el-form同用并校驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Vue后臺管理系統(tǒng)權(quán)限控制與動態(tài)路由的實(shí)現(xiàn)
本文主要介紹了Vue后臺管理系統(tǒng)權(quán)限控制與動態(tài)路由的實(shí)現(xiàn),可以根據(jù)用戶的角色靈活控制頁面訪問權(quán)限,提高系統(tǒng)的安全性和用戶體驗(yàn),感興趣的可以了解一下2025-04-04
詳解keep-alive + vuex 讓緩存的頁面靈活起來
這篇文章主要介紹了keep-alive + vuex 讓緩存的頁面靈活起來,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Vue組件通信入門之Provide和Inject機(jī)制
這篇文章主要給大家介紹了關(guān)于Vue組件通信入門之Provide和Inject機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Vue組件通信具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
element ui中表單el-form的label如何設(shè)置寬度
這篇文章主要介紹了element ui中表單el-form的label如何設(shè)置寬度,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

