Within my project, I implemented a function that handles file downloads. This function, onDownload
, is triggered when the download button is clicked:
import {useOnDownload} from "../../use/useOnDownload"
setup() {
...
const loading = ref(null)
onDownload = async (id) => {
loading.value = id
await useOnDownload(id)
loading.value = null
}
return {loading, onDownload}
}
To optimize code reusability, I centralized the API-related logic by creating a separate file named useOnDownload.js
. This allows other components to utilize the same functionality.
export async function useOnDownload(id) {
// make api call to server using axios
}
The issue I encountered is needing to properly handle the asynchronous nature of the useOnDownload
function in order for the loader to function correctly.