'Export method from imported module
Consider the following example where I import a module with an indefinite number of items:
import * as module from './module.js';
const something = () => {
// Also doing something with other methods from the imported module.
return module.method1() * 2;
}
export {
something as default,
module.method2 as method2 // THIS DOES NOT WORK!!
}
I want to export method2 from the imported module, but I can't make it work.
Also I don't benefit from destructuring because I also use an indefinite number of methods in the code (not pictured in the example).
How can I achieve this?
Can I export it directly without assigning the method2 to a new variable before exporting?
Also worth nothing that I might use the same method name or not.
Does this influence the outcome, apart from using x as y?
Solution 1:[1]
You're probably looking for a separate
export { method2 } from './module.js';
// or
export { method2 as methodAlias } from './module.js';
// or with a local identifier:
import { method2 } from './module.js';
export { method2 }
If you only want to refer to the module namespace object, and not repeat the imported module specifier, then yes you need to create an extra variable to export it.
export const method2 = module.method2;
// or
export const { method2 } = module;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bergi |
