javascript - 为什么生成的 javascript 代码(基于 typescript )给出错误,即 "class"语句

标签 javascript visual-studio-2013 typescript

我决定在我正在开发的某些应用程序中使用 typescript 而不是我不喜欢的javascript,不幸的是我收到了一些奇怪的(至少对我来说)错误。我开发了一些非常小的“应用程序”来检查是否会发生同样的错误。不幸的是它们发生了......

我正在使用 AMD、ecmascript 6 和 Visual Studio 2013。

这是我的根 html 文件:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>TypeScript HTML App</title>
    <link rel="stylesheet" href="app.css" type="text/css" />
    <script data-main="start" type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.1/require.min.js"></script>
</head>
<body>
    <h1>TypeScript HTML App</h1>
    <div id="content"></div>
</body>
</html>

我还有一些用 typescript 编写的非常简单的类:

export class trol {
    nazwa: string = "trolisko";
    echo() {
        alert(this.nazwa);
    }
}

没有有关任何错误或警告的信息。 VS将其转化为这样的javascript代码:

define(["require", "exports"], function (require, exports) {
    "use strict";
    class trol {
        constructor() {
            this.nazwa = "trolisko";
        }
        echo() {
            alert(this.nazwa);
        }
    }
    exports.trol = trol;
});
//# sourceMappingURL=trol.js.ma

但是当我运行“应用程序”时,我收到一些错误:错误消息中的类语句带有下划线,其中写有语法中存在一些错误...

下面我将放置一些屏幕截图:

[![enter image description here][1]][1]
[![enter image description here][2]][2]
[![enter image description here][3]][3]
 [![enter image description here][4]][4]

我做错了什么?

最佳答案

What am I doing wrong?

您已将 EcmaScript 版本设置为 EcmaScript 6。请设置为 EcmaScript 5,因为您的目标浏览器不支持它。

PS:这是一本免费的书,可能会对您有更多帮助 https://basarat.gitbooks.io/typescript/content/docs/getting-started.html

关于javascript - 为什么生成的 javascript 代码(基于 typescript )给出错误,即 "class"语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38768044/

相关文章:

javascript - Firefox 4b11 版本 mywebsite 图片、css、js 未加载

visual-studio-2013 - 有没有办法在没有管理员权限的情况下将站点从 Visual Studio 发布到 IIS?

visual-studio-2013 - Visual Studio 2013 不会以管理员身份运行

typescript - 接受任何对象作为函数中的参数

typescript - 单元测试返回或使用 promise 的 ionic 函数 - TS2304 : Cannot find name 'done' - Jasmine/Karma for unit testing ionic apps

javascript - 如何在 droppable 和 sortable 之间拖放元素(两种方式)?

javascript - 将文本复制到剪贴板在 Bootstrap 模式对话框中不起作用

javascript - 如何使用 THREE.JS 从 xyz 中的点数组制作自定义形状?

javascript - Typescript 2 无法解析外部 Node 模块

tfs - Visual Studio 2013 在右键单击时挂起