2017-02-13 47 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 –