admin管理员组文章数量:1129050
Using ES6 modules, I know I can alias a named import:
import { foo as bar } from 'my-module';
And I know I can import a default import:
import defaultMember from 'my-module';
I'd like to alias a default import and I had thought the following would work:
import defaultMember as alias from 'my-module';
But that results in a parsing (syntax) error.
How can I (or can I?) alias a default import?
Using ES6 modules, I know I can alias a named import:
import { foo as bar } from 'my-module';
And I know I can import a default import:
import defaultMember from 'my-module';
I'd like to alias a default import and I had thought the following would work:
import defaultMember as alias from 'my-module';
But that results in a parsing (syntax) error.
How can I (or can I?) alias a default import?
Share Improve this question edited Oct 3, 2020 at 18:16 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Sep 1, 2016 at 23:24 sfletchesfletche 49.6k31 gold badges108 silver badges120 bronze badges2 Answers
Reset to default 1797defaultMember
already is an alias - it doesn't need to be the name of the exported function/thing. Just do
import alias from 'my-module';
Alternatively you can do
import {default as alias} from 'my-module';
but that's rather esoteric.
Origins, Solution and Answer:
Background:
A module can export functionality or objects from itself for the use in other modules
The Modules Used for:
- Code reuse
- Separation of functionalities
- Modularity
What are Import Aliases?
Import aliases are where you take your standard import, but instead of using a pre-defined name by the exporting module, you use a name that is defined in the importing module.
Why is this important?
You may be importing multiple exported modules but the names of the exports (from different modules) are the same, this confuses JS. Aliases solve this.
Example of Multiple Alias Failed Compilation:
Failed to compile.
/somepath/index.js
SyntaxError: /somepath/index.js: Identifier 'Card' has already been declared (6:9)
Importing Aliases will allow you to import similarly named exports to your module.
import { Button } from '../components/button'
import { Card } from '../components/card'
import { Card } from 'react-native-elements'
When importing named exports (not default):
// my-module.js
function functionName(){
console.log('Do magic!');
}
export { functionName };
Import in module:
import { functionName as AliasFunction} from "my-module.js"
What is default export?
Default export allows us to export a single value or to have a fallback value for your module.
For importing default exports:
// my-module.js
function functionName(){
console.log('Do magic!');
}
export default functionName;
Solution
The defaultMember
mentioned in the question is an alias already, you can change the name to whatever you will like.
Now import the exported function (functionName()
);
import AliasFunction from "my-module.js"
Or like this (as mentioned by @Bergi):
import {default as AliasFunction} from 'my-module.js';
本文标签: ecmascript 6How can I alias a default import in JavaScriptStack Overflow
版权声明:本文标题:ecmascript 6 - How can I alias a default import in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736690570a1947904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论