Error Duplicate Const Declaration in Switch Case Statement

前端 未结 6 1653
别跟我提以往
别跟我提以往 2020-11-27 05:07

I have the following code and I get the error \'Duplicate Declaration query_url\'.

  switch(condition) {
    case \'complex\':
      const query_url = `somet         


        
6条回答
  •  甜味超标
    2020-11-27 05:40

    if you need to redeclare the same variable in each case see @Bergi 's answer bellow

    if query_url can have multiple values depending on the switch branch obviously you need a variable ( declare either with var or let ).

    const is set once and stays that way.

    example usage with let

    let query_url = '';
    switch(condition) {
      case 'complex':
        query_url = `something`;
        break;
      default:
        query_url = `something`;
        break;
    }
    

提交回复
热议问题