在Switch案例声明中出现重复的Const声明错误 [英] Error Duplicate Const Declaration in Switch Case Statement
本文介绍了在Switch案例声明中出现重复的Const声明错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,我收到错误'Duplicate Declaration query_url'。
I have the following code and I get the error 'Duplicate Declaration query_url'.
switch(condition) {
case 'complex':
const query_url = `something`;
break;
default:
const query_url = `something`;
break;
}
我知道query_url正在声明两次是不对的。但我不知道如何解决这个问题。有人可以帮助您做出正确的方法吗?
I understand that query_url is getting declared twice which isn't right. But i don't know how to resolve this. Can someone please help on what should be the correct way to make this work?
推荐答案
if query_url
可以有多个值,具体取决于交换机分支显然需要一个变量(声明为$ code> var 或 let
)。
if query_url
can have multiple values depending on the switch branch obviously you need a variable ( declare either with var
or let
).
const设置一次并保持这种方式。
const is set once and stays that way.
p>
example usage with let
let query_url = '';
switch(condition) {
case 'complex':
query_url = `something`;
break;
default:
query_url = `something`;
break;
}
这篇关于在Switch案例声明中出现重复的Const声明错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文