WXL
2024-11-27 a520895c5b01934a7210917d52cbe98455cba33b
src/components/Address/index.vue
@@ -50,18 +50,20 @@
<script>
// 使用说明:v-model时,必须传带有带有省,市,区拼音的字段
import request from '@/utils/request'
import request from "@/utils/request";
import store from "@/store";
export default {
  name: "li_area_select",
  //通过 model 选项配置子组件接收的 prop 名以及派发的事件名
  model: {
    prop: "caddress",
    event: "change",
    event: "change"
  },
  props: {
    caddress: {
      type: Object,
    },
      type: Object
    }
  },
  data() {
    return {
@@ -69,7 +71,7 @@
      //areaJson: './../address.json',
      addressArray: [], //所有数据
      cityArray: [],
      areaArray: [],
      areaArray: []
    };
  },
  created() {
@@ -78,9 +80,32 @@
  methods: {
    getAddressData() {
      var that = this;
      console.log(store.getters.addressArray,'addressArray');
      if (store.getters.addressArray.length) {
        that.addressArray = store.getters.addressArray.length;
        //默认值赋值获取城市数组
        if (that.caddress.sheng) {
          for (let ad of that.addressArray) {
            if (ad.areaname === that.caddress.sheng) {
              that.cityArray = ad.subarea;
              //---
              //默认赋值获取区域数组
              if (that.caddress.shi) {
                for (let area of that.cityArray) {
                  if (area.areaname === that.caddress.shi) {
                    that.areaArray = area.subarea;
                    break;
                  }
                }
              }
            }
          }
        }
      } else {
      request({
        url: that.areaJson,
        method: "get",
          method: "get"
      }).then(function (response) {
        if (response.code === 200) {
          //获取地址
@@ -106,6 +131,7 @@
          }
        }
      });
      }
    },
    //选择省份
    getCityData(val) {
@@ -138,30 +164,29 @@
    },
    getSheng(){
      let list= this.addressArray.filter(r=>r.areaname ==  this.caddress.sheng);
      let list = this.addressArray.filter(
        r => r.areaname == this.caddress.sheng
      );
      if(list.length>0){
        return list[0].areacode;
      }
      else{
        return '';
      } else {
        return "";
      }
    },
     getShi(){
      let list= this.cityArray.filter(r=>r.areaname ==  this.caddress.shi);
      if(list.length>0){
        return list[0].areacode;
      }
      else{
        return '';
      } else {
        return "";
      }
    },
     getQu(){
      let list= this.areaArray.filter(r=>r.areaname ==  this.caddress.qu);
      if(list.length>0){
        return list[0].areacode;
      }
      else{
        return '';
      } else {
        return "";
      } 
    },
    clean(){
@@ -169,9 +194,8 @@
      this.caddress.shi="";
      this.caddress.qu="";
    }
  },
  }
};
</script>
<style scoped>
</style>
<style scoped></style>