Use exported values ​​in the settings of the Vue Composition API
P粉237125700
P粉237125700 2023-08-25 21:31:18
0
1
444
<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>
P粉237125700
P粉237125700

reply all(1)
P粉245003607

You can try to declare the fetchEvents and isEventActive methods in the planned js file without wrapping them in any function

const fetchEvents = () => {
   //body
};

const isEventActive = () => {
     //body
};

and export them as

export {fetchEvents, isEventActive};

Use them now

import {fetchEvents,isEventActive} from 'path-to-js-file'
export default {
  setup() {
    fetchEvents()
    isEventActive()
  }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template