anji-fourLevel.vue
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<template>
<el-cascader ref="country" v-model="countryValue" size="mini" :props="props" :options="countryOptions" style="width: 100%" clearable @change="handleChange" />
</template>
<script>
import { postRequest } from '@/api/common.js'
export default {
props: {
value: null,
},
data() {
return {
countryValue: [],
props: {
value: 'value',
label: 'label',
children: 'children',
},
countryOptions: [],
}
},
watch: {
value(val) {
this.handleCountryEcho(val)
},
},
created() {
this.provinceFn()
this.handleCountryEcho(this.value)
},
methods: {
// 获取省市区
async provinceFn() {
const { code, data } = await postRequest()
if (code != 200) return
this.countryOptions = data
},
handleChange(value) {
if (value.length > 0) {
const countryNameArr = this.$refs.country.getCheckedNodes()[0].pathLabels
const countryObj = {
countryName: countryNameArr[0],
provinceName: countryNameArr[1],
cityName: countryNameArr[2],
areaName: countryNameArr[3],
countryCode: value[0],
provinceCode: value[1],
cityCode: value[2],
areaCode: value[3],
}
this.$emit('input', countryObj)
this.$emit('change', countryObj)
} else {
this.$emit('input', {})
this.$emit('change', {})
}
},
handleCountryEcho(val) {
if (val) {
this.countryValue = [val.countryCode, val.provinceCode, val.cityCode, val.areaCode || val.regionCode]
} else {
this.countryValue = []
}
},
},
}
</script>
<style lang="scss" scoped>
.con {
display: inline-block;
}
</style>