url_select.vue 1022 Bytes
Newer Older
水落(YangLei)'s avatar
水落(YangLei) committed
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
<template>
    <a-select
        style="width: 100%"
        :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
        placeholder="Please select"
        allow-clear
        :options="options"
        v-on="$listeners"
        v-bind="$attrs"
    />
</template>

<script>
import { getReq } from '@/utils/requestUtil';
import { formatObj } from '@/utils';

export default {
    props: {
        url: String,
        labelFiled: String,
        valueFiled: String,
    },
    data: () => ({
        options: [],
    }),

    model: {
        prop: 'value',
        event: 'change',
    },

    watch: {
        url: 'getData',
    },

    methods: {
        async getData() {
            if (this.url) {
                this.options = formatObj(await getReq(this.url), {
                    label: this.labelFiled,
                    value: this.valueFiled,
                    key: this.valueFiled,
                });
            }
        },
    },

    mounted() {
        this.getData();
    },
};
</script>