我有一个函数,它根据传入的特定规则检查顺序流中的当前阶段,并根据该值在Angular 2应用程序中分配下一个值。看起来像这样:

private getNextStageStep(currentDisciplineSelected) {
    const nextStageStep = '';
        if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 1') {
            const nextStageStep = 'step 2';
        } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 2') {
            const nextStageStep = 'step 3';
        } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 3') {
            const nextStageStep = 'step 4';
        } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 4') {
            const nextStageStep = 'step 5';
        } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 5') {
            const nextStageStep = 'step 6';
    }
    return nextStageStep;
}
我在这里所做的是返回nextStageStep的值,因为这是为了使正确的阶段步骤发生而要传递的内容。
现在,我的tslint正在使用nextStageStep警告为每个no shadowed variables变量出现强调。如果我删除初始化为空字符串的行,那么警告就会消失,但是我得到了错误,Cannot find nextStageStep显示在我的return语句中。
原始阴影变量警告有什么问题,有没有替代的写法,和/或在这种情况下我是否应该忽略tslint警告?

最佳答案

linter提示,因为您多次重新定义了相同的变量。因此,替换包含它的封闭中的那些。

不用重新声明它,只需使用它:

private getNextStageStep(currentDisciplineSelected) {
    let nextStageStep = '';
        if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 1') {
             nextStageStep = 'step 2';
        } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 2') {
             nextStageStep = 'step 3';
        } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 3') {
             nextStageStep = 'step 4';
        } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 4') {
             nextStageStep = 'step 5';
        } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 5') {
             nextStageStep = 'step 6';
    }
    return nextStageStep;
}

关于javascript - 了解 'No Shadowed Variable' tslint警告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44853057/

10-10 00:28
查看更多