index.vue 1.39 KB
<template>
  <el-cascader :props="props" v-model="innerValue" :options="options" v-bind="$attrs"
    v-on="$listeners" @change="handleChange" class="address-cascader" />
</template>

<script>
import { getProvinceSelector } from '@/api/system/province'
export default {
  name: 'NCC-Address',
  props: ["value", "level"],
  model: {
    prop: 'value',
    event: 'change'
  },
  data() {
    let that = this;
    return {
      options: [],
      props: {
        lazy: true,
        value: 'value',
        label: 'label',
        children: 'children',
        leaf: 'leaf',
        lazyLoad(node, resolve) {
          const { data, level } = node;
          let id = level === 0 ? -1 : data.value
          getProvinceSelector(id).then(res => {
            const list = res.data.list.map((value, i) => ({
              value: value.id,
              label: value.fullName,
              leaf: level >= that.level ? true : value.isLeaf
            }));
            resolve(list);
          })
        }
      },
      innerValue: this.value
    }
  },
  methods: {
    handleChange(e){
      console.log(e);
    },
  },
  created() { },
  watch: {
    innerValue(val) {
      console.log('innerValue',val);
      val && this.$emit('change', val.join('-'))
    },
    value(val) {
      this.innerValue = val && val.split('-') || [];
    }
  }
}
</script>
<style lang="scss" scoped>
.address-cascader {
  width: 100%;
}
</style>