【问题标题】:Not all typescript files compile to js using atom-typescript并非所有打字稿文件都使用 atom-typescript 编译为 js
【发布时间】:2015-09-10 20:07:58
【问题描述】:

我正在关注在 Angular2 https://www.youtube.com/watch?t=10&v=ZsGRiHSaOxM 中构建路由的教程,它显示了使用 .ts 构建的部分页面,并且在每种情况下都有一个配套的 .js 文件。我的带有 atom-typescript 的 Atom 编辑器在我更改 app.ts 文件时将其编译为 app.js,但据我所知,我在另一个目录中的部分页面无法编译。

当文件是homePage.ts时,控制台错误是-找不到homePage.js 当文件是homePage.js时,控制台错误是-unexpected token @ on line(@Component在哪里)。我的 tsconfig 中有“compileOnSave”:true,并且该文件位于顶级项目目录中。

我尝试将fileGlob放入tsconfig,来自文档,如下

"filesGlob": [
    "./**/*.ts",
    "!./node_modules/**/*.ts"
],

没有任何改变。

我的编译器选项是:

"compilerOptions": {
    "charset": " UTF-8",
    "declaration": false,
    "diagnostics": true,
    "emitDecoratorMetadata": true,
    "module": "commonjs",
    "noImplicitAny": false,
    "noLib": false,
    "removeComments": true,
    "sourceMap": true,
    "target": "es5",
    "version": true
},

我是否需要所有组件的 .ts 和 .js 文件,如果需要,如何使用 atom-typescript 实现这一点?或者为什么我会收到意外的令牌@错误?

【问题讨论】:

  • @ 的错误是跟踪器问题。你把它添加到你的项目中了吗?在你的视频中,这个人做到了。是的,你需要这两个文件:你用 typescript 编写,编译器将它转换成常规的 javascript(这是浏览器真正使用的)。
  • 检查一下,这是一个使用 Angular 2 和 Typescript 启动和运行的最小项目,由 Angular 团队成员维护 -> github.com/pkozlowski-opensource/ng2-play.ts
  • 埃里克,谢谢。是的,我在 index.html 页面中有其他脚本加载的 traceur,如下 -
  • jhadesdev,谢谢。那可能会奏效——科兹洛夫斯基知道他的东西。我的目标是了解有关构建 Angular2 的详细信息以及将 Atom 与 atom-typescript 一起使用,因为我有一种预感,所有这些都是未来的光明浪潮。

标签: typescript atom-editor angular


【解决方案1】:

或者为什么我得到了意想不到的令牌@error

您需要将experimentalDecorators 设置为true。所以:

"compilerOptions": {
    "charset": " UTF-8",
    "declaration": false,
    "diagnostics": true,
    "experimentalDecorators", true,
    "emitDecoratorMetadata": true,
    "module": "commonjs",
    "noImplicitAny": false,
    "noLib": false,
    "removeComments": true,
    "sourceMap": true,
    "target": "es5",
    "version": true
},

FWIW 这就是装饰器在 TypeScript 编译器中的实现方式?

【讨论】:

  • 我将 tsconfig 更改为上述内容,加上“compileOnSave”:true(另外,它应该是“experimentalDecorators”:true,)经过多次尝试,Atom 编译并制作了 .js 文件的 1 个路由.我制作了第二个部分页面 .ts 文件,但我还不能让 Atom 编译它。我反复收到控制台错误 - 未找到 partialPage2.js,当我在 Atom 中进行更改时,我没有看到描述编译状态的黄色或绿色框。它发出,它说我没有错误,但只是偶尔编译。有什么想法吗?我可能有代码错误,很快就会检查。
  • 添加“experimentalDecorators”:true,修复了意外的标记@错误,并且由于某种原因,atom-typescript 现在每次编译都没有错误。谢谢。现在修复我的路由器错误。
猜你喜欢
  • 1970-01-01
  • 2014-07-04
  • 1970-01-01
  • 2017-03-26
  • 2016-04-01
  • 2013-03-29
  • 1970-01-01
  • 2012-10-07
  • 1970-01-01
相关资源
最近更新 更多