2017-02-13 56 views
0

我想學習打字稿&想要使用模塊加載器(system.js)但我碰到了一個我似乎無法解決的錯誤。毫無疑問,我沒有正確配置一些東西。我有2個文件,其中之一是我期望導入的模塊。我曾嘗試導入變量&類,但錯誤始終存在。如果有人能給我一個關於我可能會出錯的指示,我會非常感激。Typescript初學者 - 使用與打字稿的system.js模塊加載器問題

enter image description here

我有2個文件一個index.html &一個tsconfig.json文件

的index.html

<html> 
<head> 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.20.7/system.js"></script> 
    <script> 
    System.import('main').then(function(m) {console.log(m)}); 
    </script> 
</head> 
<body> 
</body> 
</html> 

main.ts沿

import {anotherName} from './test'; 

let name: string = "bob"; 

console.log(name); 
console.log(anotherName); 

test.ts

export let anotherName: string = "Jeff"; 

main.js

System.register(["./test"], function (exports_1, context_1) { 
    "use strict"; 
    var __moduleName = context_1 && context_1.id; 
    var test_1, name; 
    return { 
     setters: [ 
      function (test_1_1) { 
       test_1 = test_1_1; 
      } 
     ], 
     execute: function() { 
      name = "bob"; 
      console.log(name); 
      console.log(test_1.anotherName); 
     } 
    }; 
}); 

tsconfig.json

{ 
    "compilerOptions": { 
     "target": "es5", 
     "module": "system", 
     "moduleResolution": "node", 
     "isolatedModules": false, 
     "jsx": "react", 
     "experimentalDecorators": true, 
     "emitDecoratorMetadata": true, 
     "declaration": false, 
     "noImplicitAny": false, 
     "noImplicitUseStrict": false, 
     "removeComments": true, 
     "noLib": false, 
     "preserveConstEnums": true, 
     "suppressImplicitAnyIndexErrors": true 
    }, 
    "exclude": [ 
     "node_modules", 
     "typings/browser", 
     "typings/browser.d.ts" 
    ], 
    "compileOnSave": true, 
    "buildOnSave": false, 
    "atom": { 
     "rewriteTsconfig": false 
    } 
} 

回答

0

你必須告訴SystemJs追加」。 js'到模塊na你正在給它mes。

System.defaultJSExtensions = true; 
+0

只有當您*不*使用SystemJS直接轉錄打字稿時纔會出現這種情況。即使你不是包配置,也應該優先設置defaultJSExtensions。也就是說,你應該設置一個包,並使用''ts「'或'」js「'包的級別'defaultExtension'來設置包的等級,具體取決於你是否使用SystemJS –