Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript reactjs语法错误:url为只读_Javascript_Reactjs_React Router - Fatal编程技术网

Javascript reactjs语法错误:url为只读

Javascript reactjs语法错误:url为只读,javascript,reactjs,react-router,Javascript,Reactjs,React Router,我在react中有这个问题我有这个函数,但它不是react的正确格式 check(img) { console.log(img,typeof img) const url=""; const arrN = ["15","16","35","36","37","38","39","40","n15","n16","n35","n36","n37","n38","n39","n40"]; for (var i = 0; i < a

我在react中有这个问题我有这个函数,但它不是react的正确格式

check(img) {
        console.log(img,typeof img)
        const url="";
        const arrN = ["15","16","35","36","37","38","39","40","n15","n16","n35","n36","n37","n38","n39","n40"];
        for (var i = 0; i < arrN.length; i++) {
            if (img === arrN[i]) {
                 url = "/blah/allIcons/blah"+img+"_en.png";
            }else{
                 url = "/blah/allIcons/blah"+img+".png";
            }
        }
        return url;
    }

我该怎么做?

如果更改
url
变量,则不应将其声明为常量。使用

check(img) {
  const arrN = ["15", "16", "35", "36", "37", "38", "39", "40", "n15", "n16", "n35", "n36", "n37", "n38", "n39", "n40"];
  let url = "";

  for (var i = 0; i < arrN.length; i++) {
    if (img === arrN[i]) {
      url = "/blah/allIcons/blah" + img + "_en.png";
    } else {
      url = "/blah/allIcons/blah" + img + ".png";
    }
  }
  return url;
}

如果更改
url
变量,则不应将其声明为常量。使用

check(img) {
  const arrN = ["15", "16", "35", "36", "37", "38", "39", "40", "n15", "n16", "n35", "n36", "n37", "n38", "n39", "n40"];
  let url = "";

  for (var i = 0; i < arrN.length; i++) {
    if (img === arrN[i]) {
      url = "/blah/allIcons/blah" + img + "_en.png";
    } else {
      url = "/blah/allIcons/blah" + img + ".png";
    }
  }
  return url;
}
使用
let url=“”
代替
const url=“”

常数的值不能通过重新赋值而改变,并且 不能重新申报

因此,如果您声明变量
const url=“
,您以后就不能说
url=“/blah/allIcons/blah”+img+“_en.png”

  • 常数
使用
让url=”“
代替
常量url=“”

常数的值不能通过重新赋值而改变,并且 不能重新申报

因此,如果您声明变量
const url=“
,您以后就不能说
url=“/blah/allIcons/blah”+img+“_en.png”

  • 常数

可能使用上面定义的
url
。只需使用
url=“”
而不是
const url=“”
@阿德纳努默提出了非常糟糕的建议。我想你不知道为什么声明变量很重要。const是你不能改变的东西,使用var或let,读取const,var,let之间的差异。可能使用上面定义的
url
。只需使用
url=“”
而不是
const url=“”
@阿德纳努默提出了非常糟糕的建议。我想你不知道为什么声明变量很重要。const是你不能改变的东西,使用var或let,读取const,var,let之间的差异。不应将其声明为常量:)不应将其声明为常量:)您能提供更多信息和详细信息吗?感谢you@Robert,我用更多的信息和参考资料更新了我的答案,但是我不会在这里详细解释
let
const
,因为问题与此无关。你能在回答中提供更多信息和细节吗?感谢you@Robert,我用更多的信息和参考资料更新了我的答案,但我不会在这里详细解释
let
const
,因为问题不是关于它的。
check(img) {
  const arrN = ["15", "16", "35", "36", "37", "38", "39", "40", "n15", "n16", "n35", "n36", "n37", "n38", "n39", "n40"];

  if (arrN.indexOf(img) > -1) { // or if (arrN.includes(img)) {...}
    return "/blah/allIcons/blah" + img + "_en.png";
  }

  return "/blah/allIcons/blah" + img + ".png";
}