Typescript初学者 - 使用带有打字稿问题的system.js模块加载器

时间:2017-02-13 22:04:01

标签: typescript module atom-editor systemjs

我正在尝试学习Typescript&想要使用模块加载器(system.js),但我遇到了一个我似乎无法解决的错误。毫无疑问,我没有正确配置。我有2个文件,其中一个是我要导入的模块。我试过导入变量&类但错误始终存在。如果有人能指出我可能出错的地方,我将非常感激。

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
    }
}

1 个答案:

答案 0 :(得分:0)

您必须告诉SystemJs追加&#39; .js&#39;到你给它的模块名称。

 System.defaultJSExtensions = true;