[英]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.