【问题标题】:Block-scoped declarations ... TypeScript and Asp.net MVC 5块范围声明 ... TypeScript 和 Asp.net MVC 5
【发布时间】:2016-01-28 22:00:58
【问题描述】:

我尝试在我的 asp.net mvc5 项目中使用 TypeScript。我创建 app.ts 文件并安装 nuget-package jquery.TypeScript.DefinitelyTyped。 app.ts 代码:

/// <reference path="typings/jquery/jquery.d.ts"/>

class TestClass {
    test(): void {
        alert("Taras TS");
    }
}

window.onload = () => {
    var tcs: TestClass = new TestClass();
    $('#taras').click(() => { tcs.test() });
};

这是非常简单的代码。之后我创建了 index.cshtml 文件,其中创建了一些带有 id='taras' 的按钮,代码:

@{
    ViewBag.Title = "tmp";
}

<button id="taras"> TS EVENT </button>

@section scripts{
    <script src="@Url.Content("~/Scripts/app.ts")"></script>
}

我认为这是健康的代码,但我在 google chrome 调试控制台中捕获了下一个异常:

app.ts:3 未捕获的 SyntaxError:块范围的声明(let、const、 函数、类)在严格模式之外尚不支持

为什么会这样?以及如何解决它,非常感谢。

【问题讨论】:

    标签: javascript c# asp.net-mvc-5 typescript


    【解决方案1】:

    Url.Content 仅将相对路径转换为绝对路径。它不会更改文件扩展名。

    浏览器无法读取您的 .ts 文件(因此出现该错误,因为它不理解 TypeScript 语法)。将文件扩展名更改为编译后的 .js 文件:

    @section scripts{
       <script src="@Url.Content("~/Scripts/app.js")"></script>
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 2017-04-05
      • 2018-01-21
      • 2023-04-08
      • 2018-05-12
      相关资源
      最近更新 更多