[Vuejs]-Dynamic import of an instance in vue?

0👍

Because the module you want is not known at runtime, you will have to import it asynchronously, else your script will have to wait for the file to be fetched and parsed..

If you don’t want to use the promise.then() route, then you can opt for this:

// do something with the 'locale module' once it is available
function sayHelloTo(locale, name) {
  console.log(locale.helloText + ' ' + name)
}

// get the module asyncly
async function loadLocale(countryCode, thenDoThis, ...optionalArgs) => {
  const locale = await import(`flatpickr/dist/l10n/${countryCode}.js`)
  thenDoThis(locale, ...optionalArgs)
})

loadLocale('DE', sayHelloTo, 'John') // runs asyncly


Leave a comment