Problem: Unexpected lexical declaration in case block . error was suddenly reported when writing code
Description: I suddenly reported Unexpected lexical declaration in case block. when I was writing code today. It was a bit boring at first. Later, I found that the type definition was written in the switch. The error code is as follows:
switch (item.value) {
// 整理
case RIGHT_MENU[0].value:
const data = 1; // 报错
break;
// 刷新
case RIGHT_MENU[1].value:
// 实现代码
break;
default:
console.warn("未知菜单");
break;
}
Solution:
This is an eslint error message, which means that a type declaration occurs unexpectedly in a case. Eslint does not allow variables to be declared in a case. It should be said that the declaration should be placed outside, and the case only performs assignments, such as:
let data=null;
switch (item.value) {
// 整理
case RIGHT_MENU[0].value:
data = 1; // 正确
break;
// 刷新
case RIGHT_MENU[1].value:
// 实现代码
break;
default:
console.warn("未知菜单");
break;
}