Use exported values in the settings of the Vue Composition API
<p>In an ordinary js file, the code is as follows: </p>
<pre class="brush:php;toolbar:false;">export default async function exportData() {
const {data} = await store
.dispatch('fetchData')
const { bookings } = data
const booking = bookings.length ? bookings[0]._id : ''
const event = {
bookingID: booking
}
// Other methods and variables
return {
.....
}
}</pre>
<p>In vue file: </p>
<pre class="brush:php;toolbar:false;">import exportData from './exportData'
export default {
setup() {
const {
fetchEvents,
isEventActive,
} = exportData()
fetchEvents()
}
}</pre>
<p>The problem is that in the vue component, the value obtained from exportData is undefined. When the export is asynchronous, an error that fetchEvents is not a function will appear. It would work fine if it wasn't async. What's the solution here? </p>