Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

javascript - 如何在Javascript中为默认导入设置别名?(How can I alias a default import in Javascript?)

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?

(我怎么能(或者我可以?)别名默认导入?)

  ask by sfletche translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

defaultMember already is an alias - it doesn't need to be the name of the exported function/thing.

(defaultMember已经别名 - 它不需要是导出的函数/东西的名称。)

Just do

(做就是了)

import alias from 'my-module';

Alternatively you can do

(或者你可以做)

import {default as alias} from 'my-module';

but that's rather esoteric.

(但这相当深奥。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...