本文介绍了Switch Case 语句中的重复常量声明错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,但收到错误重复声明 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?
推荐答案
如果您需要在每种情况下重新声明相同的变量,请参阅 @Bergi 的回答如下
如果 query_url
可以根据 switch 分支有多个值,显然你需要一个变量(用 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.
使用 let 的示例
let query_url = '';
switch(condition) {
case 'complex':
query_url = `something`;
break;
default:
query_url = `something`;
break;
}
这篇关于Switch Case 语句中的重复常量声明错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!