Address 地址选择请使用手机扫码体验

选择自定义地址

  1. html
    <div class="address-list init" @click="showAddress">
  2. <div class="titile">选择地址</div>
  3. <div class="choose"></div>
  4. </div>
  5. <nut-address
  6. v-model="showPopup"
  7. :province="province"
  8. :city="city"
  9. :country="country"
  10. :town="town"
  11. customAddressTitle="请选择所在地区"
  12. @onChange="onChange1"
  13. @close="close1">
  14. </nut-address>
  1. javascript
  2. <script>
  3. export default {
  4. data() {
  5. return {
  6. showPopup:false,
  7. province:[{"id":1,"name":"北京"},{"id":2,"name":"广西"},{"id":3,"name":"江西"},{"id":4,"name":"四川"}], // 省
  8. city:[{"id":7,"name":"朝阳区"},{"id":8,"name":"崇文区"},{"id":9,"name":"昌平区"},{"id":6,"name":"石景山区"}],// 市
  9. country:[{"id":3,"name":"八里庄街道"},{"id":9,"name":"北苑"},{"id":4,"name":"常营乡"}],// 县
  10. town:[], // 镇 必传值:name
  11. }
  12. },
  13. methods: {
  14. showAddress(){
  15. this.showPopup = true
  16. },
  17. onChange1(cal){
  18. // 判断下一个行政区列表的内容是否有值
  19. if(this[cal.next].length < 1){
  20. this.showPopup = false
  21. }
  22. },
  23. close1(val){
  24. this.text1 = val.data.addressStr
  25. }
  26. }
  27. }
  28. </script>

选择已有地址

  1. html
    <div class="address-list other" @click="showAddressExist">
  2. <div class="titile">选择地址</div>
  3. <div class="choose"></div>
  4. </div>
  5. <nut-address
  6. v-model="showPopupExist"
  7. type="exist"
  8. :existAddress="existAddress"
  9. @onChange="onChange2"
  10. @close="close2"
  11. :isShowCustomAddress="false"
  12. existAddressTitle="配送至"
  13. @selected="selected2">
  14. </nut-address>
  1. javascript
  2. <script>
  3. export default {
  4. data() {
  5. return {
  6. showPopupExist:false,
  7. existAddress:[
  8. {"id":1,"addressDetail":"th ","cityName":"石景山区","countyName":"城区","provinceName":"北京","selectedAddress":true,"townName":""},
  9. {"id":2,"addressDetail":"12_ ","cityName":"电饭锅","countyName":"扶绥县","provinceName":"北京","selectedAddress":false,"townName":""},
  10. {"id":3,"addressDetail":"发大水比 ","cityName":"放到","countyName":"广宁街道","provinceName":"钓鱼岛全区","selectedAddress":false,"townName":""},
  11. {"id":4,"addressDetail":"还是想吧百度吧 ","cityName":"研发","countyName":"八里庄街道","provinceName":"北京","selectedAddress":false,"townName":""},
  12. ],
  13. text2:'请选择地址',
  14. }
  15. },
  16. methods: {
  17. showAddressExist(){
  18. this.showPopupExist = true
  19. },
  20. onChange2(cal){
  21. console.log('change2',cal)
  22. if(this[cal.next].length < 1){
  23. this.showPopupOther = false
  24. }
  25. },
  26. close2(val){
  27. console.log(val)
  28. if(val.type == 'exist'){
  29. this.text2 = val.data.provinceName+val.data.cityName+val.data.countyName+val.data.townName+val.data.addressDetail
  30. }else{
  31. this.text2 = val.data.addressStr
  32. }
  33. },
  34. selected2(prevExistAdd,nowExistAdd,arr){
  35. console.log(prevExistAdd)
  36. console.log(nowExistAdd)
  37. },
  38. }
  39. }
  40. </script>

自定义图标

  1. html
    <div class="address-list other" @click="showCustomImg">
  2. <div class="titile">选择地址</div>
  3. <div class="choose"></div>
  4. </div>
  5. <nut-address
  6. v-model="showPopupCustomImg"
  7. type="exist"
  8. :existAddress="existAddress"
  9. @onChange="onChange3"
  10. @close="close3"
  11. :isShowCustomAddress="false"
  12. @selected="selected3"
  13. :defaultIcon="defaultIcon"
  14. :selectedIcon='selectedIcon'>
  15. </nut-address>
  1. javascript
  2. <script>
  3. export default {
  4. data() {
  5. return {
  6. showPopupCustomImg:false,
  7. selectedIcon:require('../../assets/svg/checked.svg'),
  8. defaultIcon:require('../../assets/svg/unchecked.svg'),
  9. existAddress:[
  10. {"id":1,"addressDetail":"th ","cityName":"石景山区","countyName":"城区","provinceName":"北京","selectedAddress":true,"townName":""},
  11. {"id":2,"addressDetail":"12_ ","cityName":"电饭锅","countyName":"扶绥县","provinceName":"北京","selectedAddress":false,"townName":""},
  12. {"id":3,"addressDetail":"发大水比 ","cityName":"放到","countyName":"广宁街道","provinceName":"钓鱼岛全区","selectedAddress":false,"townName":""},
  13. {"id":4,"addressDetail":"还是想吧百度吧 ","cityName":"研发","countyName":"八里庄街道","provinceName":"北京","selectedAddress":false,"townName":""},
  14. ],
  15. tex3:'请选择地址',
  16. }
  17. },
  18. methods: {
  19. showCustomImg(){
  20. this.showPopupCustomImg = true
  21. },
  22. onChange3(cal){
  23. console.log('change2',cal)
  24. if(this[cal.next].length < 1){
  25. this.showPopupOther = false
  26. }
  27. },
  28. close3(val){
  29. console.log(val)
  30. if(val.type == 'exist'){
  31. this.text3 = val.data.provinceName+val.data.cityName+val.data.countyName+val.data.townName+val.data.addressDetail
  32. }else{
  33. this.text3 = val.data.addressStr
  34. }
  35. },
  36. selected3(prevExistAdd,nowExistAdd,arr){
  37. console.log(prevExistAdd)
  38. console.log(nowExistAdd)
  39. },
  40. }
  41. }
  42. </script>

自定义地址与已有地址切换

  1. html
    <div class="address-list other" @click="showAddressOther">
  2. <div class="titile">选择地址</div>
  3. <div class="choose"></div>
  4. </div>
  5. <nut-address
  6. v-model="showPopupOther"
  7. type="exist"
  8. :existAddress="existAddress"
  9. :province="province"
  10. :city="city"
  11. :country="country"
  12. :town="town"
  13. customAndExistTitle="选择其他地址"
  14. @onChange="onChange4"
  15. @close="close4"
  16. @selected="selected4"
  17. @switchModule="switchModule">
  18. </nut-address>
  1. javascript
    <script>
  2. export default {
  3. data() {
  4. return {
  5. showPopupOther:false,
  6. existAddress:[
  7. {"id":1,"addressDetail":"th ","cityName":"石景山区","countyName":"城区","provinceName":"北京","selectedAddress":true,"townName":""},
  8. {"id":2,"addressDetail":"12_ ","cityName":"电饭锅","countyName":"扶绥县","provinceName":"北京","selectedAddress":false,"townName":""},
  9. {"id":3,"addressDetail":"发大水比 ","cityName":"放到","countyName":"广宁街道","provinceName":"钓鱼岛全区","selectedAddress":false,"townName":""},
  10. {"id":4,"addressDetail":"还是想吧百度吧 ","cityName":"研发","countyName":"八里庄街道","provinceName":"北京","selectedAddress":false,"townName":""},
  11. ], // 已有地址列表 必传值:provinceName、cityName、countyName、townName、addressDetail 、selectedAddress
  12. province:[{"id":1,"name":"北京"},{"id":2,"name":"广西"},{"id":3,"name":"江西"},{"id":4,"name":"四川"}], // 省
  13. city:[{"id":7,"name":"朝阳区"},{"id":8,"name":"崇文区"},{"id":9,"name":"昌平区"},{"id":6,"name":"石景山区"}],// 市
  14. country:[{"id":3,"name":"八里庄街道"},{"id":9,"name":"北苑"},{"id":4,"name":"常营乡"}],// 县
  15. town:[], // 镇 必传值:name
  16. };
  17. },
  18. methods: {
  19. showAddressOther(){
  20. this.showPopupOther = true
  21. },
  22. onChange4(cal){
  23. if(this[cal.next].length < 1){
  24. this.showPopupOther = false
  25. }
  26. },
  27. close4(val){
  28. if(val.type == 'exist'){
  29. this.text2 = val.data.provinceName+val.data.cityName+val.data.countyName+val.data.townName+val.data.addressDetail
  30. }else{
  31. this.text2 = val.data.addressStr
  32. }
  33. },
  34. selected4(prevExistAdd,nowExistAdd,arr){
  35. console.log(prevExistAdd)
  36. console.log(nowExistAdd)
  37. },
  38. switchModule(cal){
  39. if(cal.type == 'custom'){
  40. console.log('点击了“选择其他地址”按钮')
  41. } else {
  42. console.log('点击了自定义地址左上角的返回按钮')
  43. }
  44. }
  45. }
  46. }
  47. </script>

API

字段说明类型默认值
v-model是否打开地址选择String‘’
type地址选择类型 exist/customStringcustom
province省,每个省的对象中,必须有 name 字段Array[]
city市,每个市的对象中,必须有 name 字段Array[]
country县,每个县的对象中,必须有 name 字段Array[]
town乡/镇,每个乡/镇的对象中,必须有 name 字段Array[]
existAddress已存在地址列表,每个地址对象中,必传值provinceName、cityName、countyName、townName、addressDetail、selectedAddress(字段解释见下)Array[]
defaultIcon已有地址列表默认图标,type=‘exist’ 时生效string‘’
selectedIcon已有地址列表选中图标,type=‘exist’ 时生效string‘’
isShowCustomAddress是否可以切换自定义地址选择,type=‘exist’ 时生效Booleantrue
customAddressTitle自定义地址选择文案,type=’custom’ 时生效string‘请选择所在地区’
existAddressTitle已有地址文案 ,type=‘exist’ 时生效string‘配送至’
customAndExistTitle自定义地址与已有地址切换按钮文案 ,type=‘exist’ 时生效string‘选择其他地址’
  • provinceName 省的名字
  • cityName 市的名字
  • countyName 县的名字
  • townName 乡/镇的名字
  • addressDetail 具体地址
  • selectedAddress 字段用于判断当前地址列表的选中项。

Event

字段说明回调参数
onChange自定义选择地址时,选择地区时触发参考 onChange
selected选择已有地址列表时触发参考 selected
close地址选择弹框关闭时触发参考 close
switchModule点击‘选择其他地址’或自定义地址选择左上角返回按钮触发{type:’exist’/‘custom’}

onChange 回调参数

参数说明可能值
custom当前点击的行政区域province(省) / city(市) / country(县) / town(乡)
next当前点击的行政区域的下一级province(省) / city(市) / country(县) / town(乡)
value当前点击的行政区域的值(返回传入的值){}

selected 回调参数

参数说明可能值
第一个参数(prevExistAdd)选择前选中的地址{}
第二个参数(nowExistAdd)当前选中的地址{}
第三个参数(arr)选择完之后的已有地址列表(selectedAddress 值发生改变){}

close 回调参数

参数说明可能值
type地址选择类型 exist/customexist/custom
data选择地址的值,custom 时,addressStr 为选择的地址组合{}

注:点击叉号或者点击遮罩层关闭地址选择,不会触发 close 事件。

Address 地址选择 - 图1