0

Is it possible to do something like

import Vue from '../vendor/vue';
    import slides from '../data/'+$root.name'+.json'; 

    Vue.component('mycomponent', {
LeBlaireau
  • 17,133
  • 33
  • 112
  • 192
  • No...for the second import. Because you can't export anything from a `.json` file. – Jai Feb 07 '18 at 13:03
  • Just copy pasting your question gave me : https://stackoverflow.com/questions/29168433/es6-variable-import-name-in-node-js – Logar Feb 07 '18 at 13:04

1 Answers1

1

No, unfortunately import statements must use static names.

There is a proposal to add a dynamic module() function that will load on the fly: https://github.com/tc39/proposal-dynamic-import

The proposed syntax is:

import('./' + dynamicFileName + '.js')
.then(module => {
    // use module
})
.catch(err => {
    // handle error
});

It looks to be currently active: https://github.com/tc39/proposals

It looks like there is a polyfill here: https://github.com/ModuleLoader/es-module-loader

Another issue is that you can't directly import json into JavaScript. The below approach might be what you're after:

Slides file:

export const slides = [
    { ... }
];

Then import the file with either a static module path or with the dynamic import polyfill.

phil.software
  • 446
  • 2
  • 5