Ember CLI and import ... as

Ember CLI and import ... as

本文关键字:as import and CLI Ember      更新时间:2023-09-26

在ember CLI中使用以下代码可以工作:

import X from 'source';
X.doSomething();

但是,使用另一种形式:

import {X as Y} from 'source';
Y.doSomething();

浏览器记录一个异常:

TypeError: Y is not defined

根据ES6规范,这应该是可行的。这种行为只是Ember CLI的限制,还是我的语法有问题?

我认为您的问题是使用了错误的导入语法。您拥有的第一个代码片段是导入默认导出,而第二个代码片段使用命名导出。看看下面的代码:

// This
import X from 'source';
// is to this
import Y from 'source';
// as this
import { X } from 'source';
// is to this
import { X as Y } from 'source';

在您的情况下,您应该使用第二个表单,因为您有一个默认导出。您也可以执行以下操作,但出于可读性的考虑,我建议您不要这样做。

import { default as Y } from 'source';