0

Lets say in my project i have a folder models and i dont know how many js-files are within it? Is there way to import all default values from each file with sintax like this or something

import * as models from './models'

so variable models would have an object where keys are files' names and values are default values or i should import from each file separately?

Thank you

Dmitry Reutov
  • 2,995
  • 1
  • 5
  • 20

1 Answers1

1

I assume you might have following structure:

models
|------> model1.js
|------> model2.js

So now Add another file called index.js as like as given below:

models
|------> model1.js
|------> model2.js
|------> index.js

And index.js file will be like:

import * as model1 from "./model1.js"
import * as model2 from "./model2.js"

export { model1, model2 };

Now you can call models/index.js file from anywhere as like as given below:

import * as models from "./models";

// models.model1
// models.model2

Hope it might solve your problem.

sabbir
  • 2,020
  • 3
  • 26
  • 48
  • Now whats the point? The same way i can collect them in my file: import * as model1 from "./model1.js"; import * as model2 from "./model2.js"; const models = { model1, model2 }; – Dmitry Reutov May 30 '20 at 15:50