这段代码报错 void function return value is used。请告诉我如何修改:var start:string = document.getElementById("getstart").addEventListener("click", function():string { select_bu01 = 0 select_bu02 = 0 select_bu03 = 0 select_bu04 = 0 select_bu05 = 0 select_y01 = 0 select_y02 = 0 select_y03 = 0 select_y04 = 0 select_y05 = 0 select_y06 = 0 select_y07 = 0 select_y08 = 0 select_y09 = 0 select_y10 = 0 select_h01 = 0 select_h02 = 0 ifMDM = 0 let tmp_data = { MDM: ifMDM, BU01: select_bu01, BU02: select_bu02, BU03: select_bu03, BU04: select_bu04, BU05: select_bu05, Year1: select_y01, Year2: select_y01, Year3: select_y01, Year4: select_y01, Year5: select_y01, Year6: select_y01, Year7: select_y01, Year8: select_y01, Year9: select_y01, Year10: select_y01 } let jsonString = JSON.stringify(tmp_data); console.log(jsonString); });
时间: 2024-04-22 21:25:30 浏览: 867
这段代码的问题在于你将一个返回类型为string的匿名函数赋值给了addEventListener方法。然而,addEventListener方法期望传入一个没有返回值的函数作为参数。
要修复这个问题,你可以将函数的返回类型声明为void,而不是string。这样,就不会再报错"void function return value is used"了。修改后的代码如下:
```typescript
var start: string = document.getElementById("getstart").addEventListener("click", function(): void {
select_bu01 = 0;
select_bu02 = 0;
select_bu03 = 0;
select_bu04 = 0;
select_bu05 = 0;
select_y01 = 0;
select_y02 = 0;
select_y03 = 0;
select_y04 = 0;
select_y05 = 0;
select_y06 = 0;
select_y07 = 0;
select_y08 = 0;
select_y09 = 0;
select_y10 = 0;
select_h01 = 0;
select_h02 = 0;
ifMDM = 0;
let tmp_data = {
MDM: ifMDM,
BU01: select_bu01,
BU02: select_bu02,
BU03: select_bu03,
BU04: select_bu04,
BU05: select_bu05,
Year1: select_y01,
Year2: select_y01,
Year3: select_y01,
Year4: select_y01,
Year5: select_y01,
Year6: select_y01,
Year7: select_y01,
Year8: select_y01,
Year9: select_y01,
Year10: select_y01
};
let jsonString = JSON.stringify(tmp_data);
console.log(jsonString);
});
```
现在,你应该不再遇到"void function return value is used"的错误了。
阅读全文