Vue實現(xiàn)省市區(qū)三級聯(lián)動el-select組件的示例代碼
更新時間:2023年02月20日 09:59:42 作者:chengqiuming
這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)省市區(qū)三級聯(lián)動el-select組件的方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的的可以參考一下
一 自定義組件
1 位置

2 代碼
<template>
<div class="areaSelect flex">
<!-- 省選擇框 -->
<el-select
filterable
:disabled="disabled"
v-model="province"
:size="size"
placeholder="省"
@change="changeCode($event,0)">
<el-option
v-for="item in provinceList"
:key="item.value"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
<!-- 市選擇框 -->
<el-select
filterable
:disabled="disabled"
class="center_select"
v-model="city"
placeholder="市"
@change="changeCode($event,1)">
<el-option
v-for="item in cityList"
:key="item.value"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
<!-- 區(qū)選擇框 -->
<el-select
filterable
:disabled="disabled"
v-model="area"
placeholder="區(qū)"
@change="changeCode($event,2)">
<el-option
v-for="item in areaList"
:key="item.value"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
</div>
</template>
<script>
export default {
name: 'regionSelect',
props: {
size: '',
disabled: {
size: String,
type: Boolean,
default: false
},
code: {
type: Object,
default: () => {
return {
areaCode: '',
areaName: '',
cityCode: '',
cityName: '',
provinceCode: '',
provinceName: ''
}
}
}
},
data () {
return {
province: '',
city: '',
area: '',
provinceList: [],
cityList: [],
areaList: []
}
},
watch: {
code (val) {
if (val.areaName && val.areaName !== '') {
this.province = val.provinceCode
this.city = val.cityCode
this.area = val.areaCode
this.provinceCity(val.provinceCode)
this.cityArea(val.cityCode)
} else {
this.cityList = []
this.areaList = []
}
}
},
mounted () {
if (this.code.areaName && this.code.areaName !== '') {
this.province = this.code.provinceCode
this.city = this.code.cityCode
this.area = this.code.areaCode
this.provinceCity(this.code.provinceCode)
this.cityArea(this.code.cityCode)
}
this.getProvince()
},
methods: {
resetArea () {
this.province = ''
this.city = ''
this.area = ''
},
// 當(dāng) type == 0 ,data 表示省編碼
// 當(dāng) type == 1 ,data 表示市編碼
changeCode (data, type) {
if (type === 0) {
this.city = ''
this.area = ''
this.provinceCity(data)
}
if (type === 1) {
this.area = ''
this.cityArea(data)
}
if (this.province !== '' && this.city !== '' && this.area !== '') {
this.$emit(
'code', [{
code: this.province,
name: this.provinceList.find(
(val) => val.value === this.province
).label
}, {
code: this.city,
name: this.cityList.find(
(val) => val.value === this.city
).label
}, {
code: this.area,
name: this.areaList.find(
(val) => val.value === this.area
).label
}]
)
}
},
// 從后臺獲得省數(shù)據(jù)列表
async getProvince () {
let result = []
let url = '/base/division/provinceList'
let data = await this.$http.get(url)
data.data.data.map((item) => {
result.push({
label: item.name,
value: item.code
})
})
this.provinceList = result
},
// 依據(jù)省編碼獲得市數(shù)據(jù)列表
async provinceCity (code) {
let result = []
let url = '/base/division/cityList'
let data = await this.$http({
url: url,
method: 'get',
params: {
provinceCode: code
}
})
data.data.data.map((item) => {
result.push({
label: item.name,
value: item.code
})
})
this.cityList = result
},
// 依據(jù)市編碼獲得區(qū)數(shù)據(jù)列表
async cityArea (code) {
let url = '/base/division/districtList'
let data = await this.$http({
url: url,
method: 'get',
params: {
cityCode: code
}
})
let result = []
data.data.data.map((item) => {
result.push({
label: item.name,
value: item.code
})
})
this.areaList = result
}
}
}
</script>
<style>
.center_select {
margin: 0 10px;
}
.global_form .areaSelect {
width: 70%;
}
.global_form .areaSelect .el-select {
width: 33.33%;
}
</style>二 main.js 配置
// 行政區(qū)劃三級選擇
import RegionSelect from './components/regionSelect'
// 行政區(qū)劃三級選擇
Vue.use(RegionSelect)
// 行政區(qū)劃三級選擇
Vue.component('regionSelect', RegionSelect)
三 使用方法
1 結(jié)構(gòu)部分
<regionSelect :code="item.value" :disabled="item.disabled" :size="layout.size" @code="changeCode($event,item.prop)" v-if="item.type==='region'" ref="selectArea" ></regionSelect>
2 代碼部分
searchForm: {
province: '', // 省
city: '', // 市
district: '' // 區(qū)
},
item: { // 省市區(qū) select 自定義組件傳參部分
value: '',
type: 'region',
disabled: false
},
layout: { // 選擇框樣式,用于傳參
size: ''
},
3 樣式部分
// 省市區(qū)選擇框改變時,傳遞出來已選擇的值
changeCode (data, prop) {
this.searchForm.province = data[0].name
this.searchForm.city = data[1].name
this.searchForm.district = data[2].name
},
// 重置選擇框
resetForm () {
this.$refs.selectArea[0].resetArea() // 清除省市區(qū)
}四 測試
1 級聯(lián)選擇

2 觀察數(shù)據(jù)

到此這篇關(guān)于Vue實現(xiàn)省市區(qū)三級聯(lián)動el-select組件的示例代碼的文章就介紹到這了,更多相關(guān)Vue省市區(qū)三級聯(lián)動組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue?3?中使用?vue-router?進行導(dǎo)航與監(jiān)聽路由變化的操作
在Vue3中,通過useRouter和useRoute可以方便地實現(xiàn)頁面導(dǎo)航和路由變化監(jiān)聽,useRouter允許進行頁面跳轉(zhuǎn),而useRoute結(jié)合watch可以根據(jù)路由變化更新組件狀態(tài),這些功能為Vue3應(yīng)用增加了靈活性和響應(yīng)性,使得路由管理更加高效2024-09-09
vue使用async/await來實現(xiàn)同步和異步的案例分享
近期項目中大量使用async,從服務(wù)器獲取數(shù)據(jù),解決一些并發(fā)傳參問題,代碼很簡單,在此也看了一些博客,現(xiàn)在async/await已經(jīng)大范圍讓使用,所以本文給大家介紹一下vue使用async/await來實現(xiàn)同步和異步的案例,需要的朋友可以參考下2024-01-01

