[]为什么字符串不能连接?

private getRandomColor():String
{
let arr:Array<String>=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
let str:String="#";
for(let i:number=0;i<6;i++){
let index:number=arr.length*Math.random();
str+=arr[index];//在IDE里面这句报错 [ts] Operator '+' cannot be applied to types 'String' and 'String'.
}
return str;
}
 
已邀请:

Laya_XS

赞同来自:

string类型  不是String类型,别搞错了,都给你提示了,你自己的字符串类型写错了!这样就行了:
 

private getRandomColor():string
{
let arr:Array<String>=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
let str:string="#";
for(let i:number=0;i<6;i++){
let index:number=arr.length*Math.random();
str += arr[index];//在IDE里面这句报错 [ts] Operator '+' cannot be applied to types 'String' and 'String'.
}
return str;
}
 

Laya_XS

赞同来自:

你按我发的在ide里就可以不报错,可以正常调试了!

Laya_XS

赞同来自:

慢慢习惯就好

要回复问题请先

商务合作
商务合作