salix-front/src/components/FetchData.vue

69 lines
1.3 KiB
Vue
Raw Normal View History

2022-10-27 12:59:19 +00:00
<script setup>
import { h, onMounted } from 'vue';
import axios from 'axios';
const $props = defineProps({
autoLoad: {
type: Boolean,
default: false,
},
url: {
type: String,
default: '',
},
filter: {
type: Object,
default: null,
},
2022-11-02 10:24:21 +00:00
where: {
type: Object,
default: null,
},
sortBy: {
type: String,
default: '',
},
limit: {
type: String,
default: '',
},
2023-12-15 01:53:47 +00:00
params: {
type: Object,
default: null,
},
2022-10-27 12:59:19 +00:00
});
2022-10-31 08:34:01 +00:00
const emit = defineEmits(['onFetch']);
2022-11-07 12:15:11 +00:00
defineExpose({ fetch });
2022-10-27 12:59:19 +00:00
onMounted(async () => {
if ($props.autoLoad) {
await fetch();
}
});
async function fetch(fetchFilter = {}) {
2023-04-18 12:43:18 +00:00
try {
const filter = Object.assign(fetchFilter, $props.filter); // eslint-disable-line vue/no-dupe-keys
2023-04-18 12:43:18 +00:00
if ($props.where) filter.where = $props.where;
if ($props.sortBy) filter.order = $props.sortBy;
if ($props.limit) filter.limit = $props.limit;
2022-11-02 10:24:21 +00:00
2023-04-18 12:43:18 +00:00
const { data } = await axios.get($props.url, {
2023-12-15 01:53:47 +00:00
params: { filter: JSON.stringify(filter), ...$props.params },
2023-04-18 12:43:18 +00:00
});
2022-10-27 12:59:19 +00:00
2023-04-18 12:43:18 +00:00
emit('onFetch', data);
} catch (e) {
//
}
2022-10-27 12:59:19 +00:00
}
const render = () => {
return h('div', []);
};
</script>
<template>
<render />
</template>