我从我的TypeScript文件中的以下导入开始.注意没有.ts扩展名意味着我正在导入一个TypeScript模块:
import githubService from './github.service';
这被转化为:
var github_service_1 = require('./github.service');
当SystemJS尝试加载此模块时,它会发出以下HTTP命令:
GET /github.service
而不是GET /github.service.js.这显然不起作用.
如何使TypeScript与SystemJS一起使用?
这是我的tsconfig.json文件:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./dist",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
},
"exclude": [
"node_modules"
],
"compileOnSave": false
}
我还尝试将上面的模块选项更改为system(将生成的模块格式从CommonJS更改为SystemJS),我仍然遇到同样的问题.
最佳答案 我认为您的问题是缺少模块名称中的js扩展名.不久前System.js改变了这一点,现在能够在没有明确指定’.js’的情况下解决它们,你必须在index.html中设置System.js选项,如下所示:
System.defaultJSExtensions = true;
欲了解更多信息:defaultJSExtensions
编辑
正如@Naresh所指出的那样,defaultJSExtensions现在已经过时了.现在看来最好的方法是在config中使用packages选项.
希望这可以帮助.