Unexpected lexical declaration in case block no-case-declarations


  • vue 项目报这个错是什么原因?

    f67d0755-26a6-455e-9f3c-fbc5ee457c95-image.png


  • 这是 eslint的校验报错的,你用的应该是严格模式,可以参考这个解决,把switch的写法写成这种就可以了

    /*eslint no-case-declarations: "error"*/
    /*eslint-env es6*/
    
    // Declarations outside switch-statements are valid
    const a = 0;
    
    switch (foo) {
        // The following case clauses are wrapped into blocks using brackets
        case 1: {
            let x = 1;
            break;
        }
        case 2: {
            const y = 2;
            break;
        }
        case 3: {
            function f() {}
            break;
        }
        case 4:
            // Declarations using var without brackets are valid due to function-scope hoisting
            var z = 4;
            break;
        default: {
            class C {}
        }
    }
    

    参考链接
    https://cloud.tencent.com/developer/section/1135658