Can a variable be passed to an import?

I’m using vue-cli and I’m wondering if there is a way to pass a variable into an import statemet?
What I mean is instead of import 'vue2-datepicker/locale/en'; if I can do soemthing like import 'vue2-datepicker/locale/{i18n.locale}';
where i18n.locale is coming from:

export const i18n = new VueI18n({
  locale: 'en', // set locale
  fallbackLocale: 'en',
  messages,
})

export default i18n;

Thanks so much!

Did you try using a template literal?

`some/path/${someVariable}`
1 Like

@lasjorg thanks for the suggestion!
I just tried but I’m getting import with a red line and "Declaration or statement expected" error.
What I tried was:

 import `vue2-datepicker/locale/${i18n.locale}`; 

I don’t think the import/export works with template literals, I should probably have checked that before suggesting it. It looks like it has to be a string literal.

15.2.2 Imports

ModuleSpecifier :
StringLiteral

1 Like