下面就为你详细讲解vue中集成省市区街四级地址组件的实现过程的完整攻略。
一、什么是省市区街四级地址组件
省市区街四级地址组件是一种常见的地址选择器,用户可以通过该组件非常方便的选择自己所在的省份、城市、区县和街道信息。
二、如何集成省市区街四级地址组件
1. 使用第三方库
可以直接使用第三方的地址组件库来快速实现,在vue中,常见的第三方库有vue-quill-editor
和vue-editor-js
等。
以vue-quill-editor
为例,首先我们需要在我们的vue项目中安装该库:
npm install vue-quill-editor -S
然后在我们的Vue组件中使用该库:
<template>
<div>
<quill-editor v-model="content"></quill-editor>
</div>
</template>
<script>
import { quillEditor } from 'vue-quill-editor'
export default {
components: {
quillEditor
},
data () {
return {
content: ''
}
}
}
</script>
这样就可以在我们的Vue组件中轻松的实现省市区街四级地址组件的功能。
2. 手动实现
如果你希望自己手动实现省市区街四级地址组件,也可以按照如下步骤:
1) 引入相关依赖
我们需要引入element-ui
这个UI库和axios
这个AJAX库,具体可以使用如下指令进行安装:
npm install element-ui axios -S
然后在我们的Vue组件中引入这两个库:
<script>
import axios from 'axios'
import { Cascader } from 'element-ui'
export default {
components: {
ElCascader
},
data() {
return {
options: [],
selectedValue: []
}
},
created() {
this.loadAreaList()
}
}
</script>
2) 加载城市信息
我们需要先通过AJAX从服务器加载城市数据,然后根据省份对城市进行分类。具体可以按照如下代码进行:
<template>
<div>
<el-cascader
:options="options"
v-model="selectedValue"
:props="props"
></el-cascader>
</div>
</template>
<script>
import axios from 'axios'
import { Cascader } from 'element-ui'
export default {
components: {
ElCascader
},
data() {
return {
options: [],
selectedValue: []
}
},
props: {
props: {
type: Object,
default: () => {
return {
value: 'id',
label: 'name',
children: 'children'
}
}
}
},
created() {
this.loadAreaList()
},
methods: {
loadAreaList() {
axios.get('/api/area/list').then(res => {
const data = res.data || []
const provinces = data.filter(item => !item.pid)
const cities = data.filter(item => item.pid && item.level === 2)
const districts = data.filter(item => item.pid && item.level === 3)
const streets = data.filter(item => item.pid && item.level === 4)
const tempProvinces = provinces.map(province => {
const citiesOfProvince = cities.filter(city => city.pid === province.id)
const tempCities = citiesOfProvince.map(city => {
const districtsOfCity = districts.filter(district => district.pid === city.id)
const tempDistricts = districtsOfCity.map(district => {
const streetsOfDistrict = streets.filter(street => street.pid === district.id)
const tempStreets = streetsOfDistrict.map(street => {
return {
id: street.id,
name: street.name
}
})
return {
id: district.id,
name: district.name,
children: tempStreets.length ? tempStreets : null
}
})
return {
id: city.id,
name: city.name,
children: tempDistricts.length ? tempDistricts : null
}
})
return {
id: province.id,
name: province.name,
children: tempCities.length ? tempCities : null
}
})
this.options = tempProvinces
})
}
}
}
</script>
通过以上步骤,就可以手动实现省市区街四级地址组件了。
三、使用示例
示例1:使用第三方库
以使用vue-quill-editor
为例,我们可以直接按照如下步骤:
1) 安装vue-quill-editor
npm install vue-quill-editor -S
2) 引入vue-quill-editor
<script>
import { quillEditor } from 'vue-quill-editor'
export default {
components: {
quillEditor
},
data () {
return {
content: ''
}
}
}
</script>
3) 在模板中使用vue-quill-editor
<template>
<div>
<quill-editor v-model="content"></quill-editor>
</div>
</template>
示例2:手动实现
以手动实现为例:
1) 引入相关依赖
<script>
import axios from 'axios'
import { Cascader } from 'element-ui'
export default {
components: {
ElCascader
},
data() {
return {
options: [],
selectedValue: []
}
},
props: {
props: {
type: Object,
default: () => {
return {
value: 'id',
label: 'name',
children: 'children'
}
}
}
},
created() {
this.loadAreaList()
}
}
</script>
2) 加载城市信息
<template>
<div>
<el-cascader
:options="options"
v-model="selectedValue"
:props="props"
></el-cascader>
</div>
</template>
<script>
import axios from 'axios'
import { Cascader } from 'element-ui'
export default {
components: {
ElCascader
},
data() {
return {
options: [],
selectedValue: []
}
},
props: {
props: {
type: Object,
default: () => {
return {
value: 'id',
label: 'name',
children: 'children'
}
}
}
},
created() {
this.loadAreaList()
},
methods: {
loadAreaList() {
axios.get('/api/area/list').then(res => {
const data = res.data || []
const provinces = data.filter(item => !item.pid)
const cities = data.filter(item => item.pid && item.level === 2)
const districts = data.filter(item => item.pid && item.level === 3)
const streets = data.filter(item => item.pid && item.level === 4)
const tempProvinces = provinces.map(province => {
const citiesOfProvince = cities.filter(city => city.pid === province.id)
const tempCities = citiesOfProvince.map(city => {
const districtsOfCity = districts.filter(district => district.pid === city.id)
const tempDistricts = districtsOfCity.map(district => {
const streetsOfDistrict = streets.filter(street => street.pid === district.id)
const tempStreets = streetsOfDistrict.map(street => {
return {
id: street.id,
name: street.name
}
})
return {
id: district.id,
name: district.name,
children: tempStreets.length ? tempStreets : null
}
})
return {
id: city.id,
name: city.name,
children: tempDistricts.length ? tempDistricts : null
}
})
return {
id: province.id,
name: province.name,
children: tempCities.length ? tempCities : null
}
})
this.options = tempProvinces
})
}
}
}
</script>
以上就是两个示例的详细说明,你可以根据实际的需求选择使用第三方库或是手动实现。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:vue中集成省市区街四级地址组件的实现过程 - Python技术站