I have multiple files called PageX
, PageY
, PageZ
etc.
Each of these have an export default SETUP { path:'blah_X', component:X }
.
In my index.js I want to import these SETUP
s into an array like this:
const PAGE_SETUPS = [
import './PageX',
import './PageY',
import './PageZ'
];
const routes = PAGE_SETUPS.map(setup => createElement(Route, {path:setup.path, component:setup.component});
I am hitting all kinds of issues. Is this possible to inline-import the default
exported item into an array like this?
When you want to export a single function, class, object, variable, array, or any other stuff, use default export. You can import the default exported component with any name.
Default exports are meant to act as a replacement for this behavior; however, the two are incompatible. TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module.
Default export - is the value that will be imported from the module, if you use the simple import statement import X from 'module'. X is the name that will be given locally to the variable assigned to contain the value, and it doesn't have to be named like the origin export. There can be only one default export.
With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.
No, there are no "inline imports" in ES6 modules. You can either use whichever kind of non-declarative import method the module loading system that you transpile to offers, or you have to spell it out:
import PageX from './PageX',
import PageY from './PageY',
import PageZ from './PageZ'
const PAGE_SETUPS = [PageX, PageY, PageZ];
What I would do is have this be in a pages directory with an index.mjs file alongside all your pages. Then you can import all your pages and export the array. From that index file you could export an array of your pages and import that in another module.
// pages/index.js
import PageX from './PageX',
import PageY from './PageY',
import PageZ from './PageZ'
export const PAGE_SETUPS = [PageX, PageY, PageZ];
Then you would just need to import your array in another file.
//some other module
import PAGE_SETUPS from './pages/index.mjs'
const routes = PAGE_SETUPS
Alternatively you could pass your modules through the original page/index.js
file instead of exporting an array.
// pages/index.js
export * from './PageX',
export * from './PageY',
export * from './PageZ'
// some other module
import {PageX, PageY, PageZ} from './pages/index.mjs'
const routes = [
PageX,
PageY,
PageZ
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With