【发布时间】:2017-10-17 12:23:15
【问题描述】:
我正在尝试将 SystemJs 与 TypeScript 结合使用,基于这篇文章:http://david-barreto.com/how-to-use-typescript-with-systemjs/
index.html
<!DOCTYPE html>
<script src="node_modules/systemjs/dist/system.js"></script>
<script src="node_modules/typescript/lib/typescript.js"></script>
<script>
System.config({
transpiler: 'typescript'
});
System.import('src/main.ts');
</script>
main.ts
function plusOne(num: number) {
return num+1;
}
alert(plusOne(1))
但是,在 Chrome 中打开 index.html 时,我看到以下错误,表明没有发生转译:
common.js:85 Uncaught (in promise) Error: Unexpected token :
Evaluating http://localhost:8080/src/main.ts
Loading src/main.ts
...
有什么问题?
【问题讨论】:
-
一般来说,我认为像这样在浏览器中进行转译并不是最佳实践。我建议通常最好在构建时运行 typescript,将输出
.js存储在浏览器从中提取的服务器上的dist(或类似)文件夹中,而不是让浏览器进入node_modules以供客户端使用-side 代码,如您的示例所示。可能存在客户端转译有用的特定情况,但它可能不是默认情况。
标签: javascript typescript systemjs jspm