我正在本地存储中保存值,如下所示
key=专业技能,value=“A、B、C”
我的test.ts文件,我已经声明了数组,但是我不能从中获取结果,代码如下所示

 getskills: Array<string> = [];
this.getskills  = localStorage.getItem("profskill");

但这是个错误-
类型“string”不能分配给类型“string[]
请帮忙,我想这样取钱-
console.log(this.getskills[0]);

最佳答案

因为,您希望逗号分隔的值表示为this.getskills的字符串数组,在split的值上使用localStorage
下面是一个示例

 //say we get the value 'a,b,c' from localStorage into the temp variable
 //var temp = localStorage.getItem(profskill);
  var temp= 'a,b,c';

 this.getskills = temp.split(',');
 console.log(this.getskills[0]);
 

关于javascript - 获取数组中的本地存储值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45977943/

10-12 06:44