Ecmascript 6 带巴别塔transpiler的const示波器

Ecmascript 6 带巴别塔transpiler的const示波器,ecmascript-6,redux,babeljs,Ecmascript 6,Redux,Babeljs,我正在我的reducer中构建react redux应用程序我在使用babel es2015和stage-2使用网页包编译bundle.js后出现了这个错误,我做了一些研究,结果发现const scope是块代码,为什么我会出现双重声明错误? 我的减速机与上面的函数类似 function print(foo){ switch(foo){ case 'test': const bar = 2; console.log(bar+1); break;

我正在我的reducer中构建react redux应用程序我在使用babel es2015和stage-2使用网页包编译bundle.js后出现了这个错误,我做了一些研究,结果发现const scope是块代码,为什么我会出现双重声明错误? 我的减速机与上面的函数类似

function print(foo){
  switch(foo){
    case 'test':
      const bar = 2;
      console.log(bar+1);
      break;
    case 'test1':
      const bar = 1;
      console.log(bar+2);
      break;
    default:
      console.log('no match')
      break;
  }
}
print('test');

您需要用花括号将每个案例分支包裹起来,如下所示:

function print(foo){
  switch(foo){
    case 'test': {
      const bar = 2;
      console.log(bar+1);
      break;
    }
    case 'test1': {
      const bar = 1;
      console.log(bar+2);
      break;
    }
  }
}

switch语句中的所有内容都是一个块范围。大括号将每种情况放在它自己的块范围内-如果没有大括号,您将在同一范围内重新声明一个常量,这将出错。

那么,您的代码中哪里有{}块?