我知道不是第一个问这个问题的人,正如我在标题中提到的,我正在尝试将字符串值转换为布尔值。
我之前已经将一些值放入本地存储,现在我想获取所有值并将所有值分配给一些布尔变量。
app.component.ts
localStorage.setItem('CheckOutPageReload', this.btnLoginNumOne + ',' + this.btnLoginEdit);
这里 this.btnLoginNumOne
和 this.btnLoginEdit
是字符串值 (“true,false”) 。
镜像.component.ts
if (localStorage.getItem('CheckOutPageReload')) {
let stringToSplit = localStorage.getItem('CheckOutPageReload');
this.pageLoadParams = stringToSplit.split(',');
this.btnLoginNumOne = this.pageLoadParams[0]; //here I got the error as boolean value is not assignable to string
this.btnLoginEdit = this.pageLoadParams[1]; //here I got the error as boolean value is not assignable to string
}
在这个组件中 this.btnLoginNumOn
e 和 this.btnLoginEdi
t 是 布尔 值;
我尝试了stackoverflow中的解决方案,但没有任何效果。
谁能帮我解决这个问题。
原文由 Zhu 发布,翻译遵循 CC BY-SA 4.0 许可协议
方法一:
方法二:
方法3:
方法四:
方法5:
来源: http ://codippa.com/how-to-convert-string-to-boolean-javascript/