search

Home  >  Q&A  >  body text

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>
P粉237125700P粉237125700463 days ago427

reply all(1)I'll reply

  • P粉245003607

    P粉2450036072023-08-26 13:44:49

    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()
      }
    }

    reply
    0
  • Cancelreply