B
B
BMaks_N12020-02-10 23:58:57
JavaScript
BMaks_N1, 2020-02-10 23:58:57

How to make such dynamic import work?

There is an idea, to load data through await import

, let's say there is a code:

async downloadData(path) {
  try {
    console.log(path)
    this._settings = await import(/* webpackMode: "eager" */path)
  } catch(err) {
    alert('Ошибка подключения файла! - ' + path)
    console.log(err)
  }

  return this
}


It does not work, but if "path" is replaced with a string, everything works, it's a matter of webpack, is it possible to implement such a method with dynamic imports? In which we pass the path and it loads something. How to point webpack to files that might be loaded in this method? is it possible? Or pull with a regular fetch?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2020-02-11
@BMaks_N1

Dynamic imports, when you pass a variable there, have a lot of nuances. I advise you to read:
https://webpack.js.org/api/module-methods/#dynamic...
https://medium.com/front-end-weekly/webpack-and-dy...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question