简体   繁体   English

打字稿错误:在严格模式下,块中不允许使用函数声明

[英]Typescript error : Function declarations are not permitted in blocks in strict mode

I try to run a script but I have this error : Function declarations are not permitted in blocks in strict mode during targeting of the 'ES3' or 'ES5' version. The modules are automatically in strict mode.我尝试运行脚本,但出现此错误: Function declarations are not permitted in blocks in strict mode during targeting of the 'ES3' or 'ES5' version. The modules are automatically in strict mode. Function declarations are not permitted in blocks in strict mode during targeting of the 'ES3' or 'ES5' version. The modules are automatically in strict mode.

I'm in typescript, here is the script :我在打字稿,这里是脚本:

export default () => {

  if(document.getElementById("myRange")){
    //slider 
    var slider = document.getElementById("myRange") as HTMLInputElement;
    var output = document.getElementById("demo");
    var gainTimeOutput = document.getElementById("time");
    var sliderEco = document.getElementById("myRange-eco") as HTMLInputElement;
    var outputEco = document.getElementById('demo-eco');
    var eco = document.getElementById('eco');
    outputEco.innerHTML = sliderEco.value;
    output.innerHTML = slider.value;

    function movingSlider(){ <-- Error happens here 
      output.innerHTML = slider.value;
      var convertSlider = parseInt(slider.value); 
      var convertSliderEco = parseInt(sliderEco.value);

      var gainTime = Math.round(convertSlider).toString();
      var gainMoney = (Math.round(convertSlider* 2).toString();

      gainTimeOutput.innerHTML = gainTime + "h"; 
      outputEco.innerHTML = sliderEco.value;
      eco.innerHTML = gainMoney + "€";
    }

    slider.oninput = e => {
      movingSlider()
    };

I don't get why, the things is that when I delete my if(document.getElementById("myRange")) , it works good, Anyone know why I have this error and how should I fix that ?我不明白为什么,问题是当我删除我的if(document.getElementById("myRange")) ,它工作得很好,有人知道我为什么有这个错误,我应该如何解决?

It's pretty much just what the error says.这几乎就是错误所说的。 You have a function declaration in a block, whose behavior is weird across environments and unpredictable before being specified in ES6, so it's not permitted by your setup.您在块中有一个函数声明,其行为在环境中很奇怪,并且在 ES6 中指定之前是不可预测的,因此您的设置不允许这样做。 Assign a function expression to a variable name instead.而是将函数表达式分配给变量名称。 Change改变

function movingSlider(){

to

const movingSlider = function movingSlider(){

If you've posted the full code of the function being exported, then the code would probably be cleaner and the above wouldn't be necessary if you simply returned early if myRange doesn't exist:如果您已经发布了要导出的函数的完整代码,那么如果myRange不存在,则代码可能会更清晰,并且如果您只是提前返回,则不需要上述代码:

export default () => {
  const slider = document.getElementById<HTMLInputElement>("myRange");
  if (!slider) {
    return;
  }
  // rest of your code

This lets you avoid an unnecessary space of indentation throughout the rest of your function.这使您可以在整个函数的其余部分避免不必要的缩进空间。

暂无
暂无

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

相关问题 “函数声明不能​​嵌套在语句中” - IE 在严格模式下的错误 - “Function declarations cannot be nested inside a statement” - IE in strict mode error Typescript和Uncaught SyntaxError:在严格模式之外尚不支持块范围的声明(let,const,function,class) - Typescript and Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode SyntaxError:严格模式不允许在词法嵌套语句中使用函数声明 - SyntaxError: Strict mode does not allow function declarations in a lexically nested statement 严格模式是否禁止语句级函数声明? - Does strict mode prohibit statement level function declarations? 在 TypeScript 中以严格模式传递给定回调函数中的其余参数 - Pass rest of arguments in a given callback function in TypeScript in strict mode AngularJS:严格模式之外尚不支持块范围的声明(let,const,函数,类) - AngularJS: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode (如果函数体处于严格模式,函数eval(){})会抛出语法错误? - (function eval () {}) throws a syntax error if function body is in strict mode? 严格模式下匿名函数的参数 - Parameters for anonymous function in strict mode 在严格模式的 typescript 项目中使用 jsbi - Using jsbi in a typescript project with strict mode 如何修复Javascript es6中的“函数声明不应放在块中”错误? - How to fix 'Function declarations should not be placed in blocks' error in Javascript es6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM