简体   繁体   English

Typescript和Uncaught SyntaxError:在严格模式之外尚不支持块范围的声明(let,const,function,class)

[英]Typescript and Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

I have a typescript file containing a class definition: 我有一个包含类定义的打字稿文件:

if (window.console == null) {
    (<any>window).console = {
            error: function (a) {
        },
            log: function (a) {
        }
    };
}

class SendMessage {
    //.....
}

After the compilation to javascript (by VS2015), I get the error on the line with the class definition: 在编译为javascript(通过VS2015)之后,我在类定义的行上得到了错误:

Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

I have found that I have to use the strict mode. 我发现我必须使用严格模式。 But why and how can I use it in typescript? 但为什么以及如何在打字稿中使用它呢?

Thanks 谢谢

It's because it's compiling to ES6 and the browser is requiring that block-scoped declarations be used in strict mode. 这是因为它正在编译ES6,浏览器要求在严格模式下使用块范围的声明。

You can fix this by using strict mode. 您可以使用严格模式来解决此问题。 To do that add... 为此,添加......

"use strict";

...to the top of every file. ...到每个文件的顶部。

However, I think you probably want to change the compilation target from ES6 to ES5. 但是,我认为您可能希望将编译目标从ES6更改为ES5。 If you are using tsconfig.json , change "target": "es6" to "target": "es5" . 如果您使用的是tsconfig.jsontsconfig.json "target": "es6"更改为"target": "es5" Doing that will...compile to ES5...and so block-scoped declarations will be changed appropriately so "use strict"; 这样做...将编译到ES5 ...所以块作用域声明将被适当更改,因此"use strict"; will not be required. 不需要。 Additionally, more browsers will support your code. 此外,更多浏览器将支持您的代码。 Right now runtime ES6 support is still not widespread. 目前,运行时ES6支持仍然不普遍。

Note that if you are not using tsconfig.json, you might have to change the target in the project properties' typescript build tab as shown here: 请注意,如果您不使用tsconfig.json,则可能必须在项目属性的typescript构建选项卡中更改目标,如下所示:

ECMAScript版本更改

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AngularJS:严格模式之外尚不支持块范围的声明(let,const,函数,类) - AngularJS: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode 在Kriasoft React样板中,严格作用域之外的严格作用域尚不支持const函数类的块范围声明 - Block-scoped declaration let const function class not yet supported outside strict mode in kriasoft react boiler plate InAppBroswer:未捕获的SyntaxError:在Android App上加载js时,尚不支持块范围的声明.. 外部严格模式 - InAppBroswer: Uncaught SyntaxError: Block-scoped declarations .. not yet supported when loading js on Android App. outside strict mode Angular:在Chrome中严格模式之外尚不支持块范围声明 - Angular: Block-scoped declarations not yet supported outside strict mode in Chrome (教程)脚本由于SyntaxError无法运行:范围限定的声明 - (Tutorial) Script does not run due SyntaxError: Block-scoped declarations 为什么在 JavaScript 中为块范围变量声明选择名称“let”? - Why was the name 'let' chosen for block-scoped variable declarations in JavaScript? 块范围的声明…TypeScript和Asp.net MVC 5 - Block-scoped declarations … TypeScript and Asp.net MVC 5 Visual Studio代码,块范围的声明 - Visual Studio Code, Block-scoped declarations SyntaxError:严格模式不允许在词法嵌套语句中使用函数声明 - SyntaxError: Strict mode does not allow function declarations in a lexically nested statement 在类实例中获取块范围变量 - get block-scoped variable in class instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM