2016-04-14 67 views
0

我在我的組件中使用SystemSystem.import使用導致我的控制檯出現多個錯誤

export class LoginComponent { 
    constructor() { 
     System.import('app/login/login.js'); 
    } 
} 

文件加載罰款,但打字稿compiller說

Error:(10, 9) TS2304: Cannot find name 'System'.

我的瀏覽器控制檯說

EXCEPTION: Error: Uncaught (in promise): Error: Error: http://localhost:3000/app/login/login.js detected as register but didn't execute. 
     at ZoneDelegate.invoke (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:332:29) 
     at Object.NgZoneImpl.inner.inner.fork.onInvoke (http://localhost:3000/node_modules/angular2/bundles/angular2.dev.js:2111:31) 
     at ZoneDelegate.invoke (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:331:35) 
     at Zone.run (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:227:44) 
    Error loading http://localhost:3000/app/login/login.js 

Error: Uncaught (in promise): Error: Error: http://localhost:3000/app/login/login.js detected as register but didn't execute.(…) 

我該如何解決呢?

+0

爲什麼在TypeScript中使用'System.import'而不是ES6導入語法?什麼是'login.js'?在哪裏找到了「LoginComponent」文件? – rgvassar

+0

@rgvassar login.js是一個用動畫支持我的頁面的腳本。如何使用ES6語法導入我的.js文件? – Viktor

回答

2

你的代碼中有一個錯誤,因爲你不必爲SystemJS圖書館分型。

大部分時間我們沒有在TypeScript文件中明確地使用System.import,因爲我們可以使它在隱藏條件下行事。我的意思是,當你使用以下,打字稿編譯器將輸入轉化成SystemJS支持的東西:

import {...} from 'app/login/login'; 

export class LoginComponent { 
    constructor() { 
    } 
} 

你可以注意到,我刪除了js擴展,因爲SystemJS允許配置默認的擴展名解析模塊時補充:

<script> 
    System.config({ 
    packages: {   
     app: { 
     format: 'register', 
     defaultExtension: 'js' 
     } 
    } 
    }); 
+0

我是TypeScript中的新成員,所以我不清楚如何從'app/login/login';'使用'import {...}導入'.js'文件。它只是說'找不到模塊'app/login/login'. – Viktor

+0

我的目標是在模板加載後動態加載'.js'。 System.import很好地處理了這個任務,但是在控制檯中引發了很多錯誤。 – Viktor

+0

您需要查找描述庫合約的SystemJS('.d.ts')文件的類型... –

0

如何導入我的.js與ES6語法文件

兩步:

  • 申報vendor.d.ts JS文件

    申報模塊「應用程序/登錄/登錄'var foo:any; export = foo; }

  • 用它在你的打字稿

    進口*從 '應用程序/註冊/登錄' 登錄;

現在只要編譯「--module系統」