如何在 Javascript 中为默认导入设置别名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39282253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I alias a default import in Javascript?
提问by sfletche
Using ES6 modules, I know I can alias a named import
使用 ES6 模块,我知道我可以别名命名导入
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?
我如何(或者我可以?)为默认导入设置别名?
回答by Bergi
defaultMember
already isan alias - it doesn't need to be the name of the exported function/thing. Just do
defaultMember
已经是一个别名 - 它不需要是导出函数/事物的名称。做就是了
import alias from 'my-module';
Alternatively you can do
或者你可以做
import {default as alias} from 'my-module';
but that's rather esoteric.
但那是相当深奥的。