mar*_*ary 1 javascript reactjs react-router
我在反应中有这个问题我有这个功能,但它的反应格式不正确
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;
}
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误
Module build failed: SyntaxError: "url" is read-only
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
如果您更改url变量,则不应将其声明为常量。使用let:
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;
}
Run Code Online (Sandbox Code Playgroud)
但是您似乎并不需要它,因为整个 for 循环检查似乎效率低下。可以这样优化:
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";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4928 次 |
| 最近记录: |