Reputation: 12500
I have a module netmap
that exports a default class NetMap
:
export default class NetMap {...}
I have another module helloworld
and I want to export (not as a default
) the entire NetMap
class so that another module can access NetMap
by using:
import * as helloworld from 'helloworld'
const x = helloworld.NetMap()
Is this possible? What would the export
of NetMap
look like in the helloworld
module?
Upvotes: 0
Views: 169
Reputation: 21161
netmap.js
export default class NetMap {
...
}
helloworld.js
(usually called a barrel):
import NetMap from './netmap.js';
import Foo from '...';
import ...
export {
NetMap,
Foo,
...
};
Then, in another module:
import * as helloworld from './helloworld.js';
const x = new helloworld.NetMap();
But I personally prefer to use named imports/exports, so I would do it like this instead:
netmap.js
export class NetMap {
...
}
helloworld.js
(usually called barrel):
export { NetMap } from './netmap.js';
export { Foo } from '...';
export { ...
Then, in another module:
import * as helloworld from './helloworld.js';
const x = new helloworld.NetMap();
Or:
import { NetMap } from './helloworld.js';
const x = new NetMap();
Upvotes: 1
Reputation: 691
I think I can tell what you're trying to do, and it certainly seems possible. But do let me know if I misunderstood.
So you have your netMap file...
// netMap.js
class NetMap {
constructor(a,b) {
this.a = a
this.b = b
}
}
export default NetMap
then you have your helloworld file that uses netmap as well as maybe some other things....
// helloworld.js
const netMap = require('./netMap')
// import netMap from 'netMap'
const helloWorld = _ => console.log('hello world!')
module.exports = { netMap, helloWorld }
export { netMap, helloWorld }
and now you have a third file for which you're going to import all of hello world...
// otherModule.js
var helloWorld = require('./helloworld')
// import * as helloWorld from 'helloworld'
const x = new helloWorld.netMap(2,3)
console.log(x.a, x.b)
Upvotes: 0