2014-10-01 90 views
2

使用燼CLI下面的代碼工作:灰燼CLI和進口......爲

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

但是,在使用的另一種形式:

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

瀏覽器登錄異常:

TypeError: Y is not defined 

根據ES6 specs這應該工作。這種行爲僅僅是Ember CLI的限制嗎,還是我的語法有問題?

回答

0

我相信你的問題是你使用了錯誤的導入語法。您擁有的第一個代碼段是導入默認導出,而第二個代碼片段使用的是命名導出。看看下面的代碼:

// 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';