Use exported values in the settings of the Vue Composition API
P粉237125700
2023-08-25 21:31:18
<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>
You can try to declare the fetchEvents and isEventActive methods in the planned js file without wrapping them in any function
and export them as
Use them now