【发布时间】:2019-02-27 18:19:45
【问题描述】:
我对 Angular 很陌生,正在尝试利用 Angular 6 构建一个电子应用程序。
我想要做的是: 1. SupportInformationClass 有几个定义 2. 在组件初始化时,从电子设置中填充定义
supportInformation.ts:
export class SupportInformation {
appsSet1: string[];
//appsSet2: string[];
//appsSet3: string[];
//appsSet4: string[];
}
configuration.componenet.ts:
import { SupportInformation } from './supportInformation';
...
...
export class ConfigurationComponent implements OnInit {
supportInformation: SupportInformation;
constructor(private childProcessService: ChildProcessService, private electronService: ElectronService) {
console.log("inside constructor...")
}
ngOnInit() {
console.log("on ngInit...");
this.getSupportedApps();
}
getSupportedApps() {
if(this.childProcessService.isElectronApp){
// this.supportInformation.appsSet1 = ["wow"] // this works
console.log(this.electronService.settings.get('APPS_1')) // this also works
this.supportInformation.appsSet1 = this.electronService.settings.get('APPS_1'); // this gives an error
}
}
}
即使 this.electronService.settings.get('APPS_1') 返回一个字符串元素数组,我也会在此特定行上出现错误。
this.supportInformation.appsSet1 = this.electronService.settings.get('APPS_1');
错误:
Type 'JsonValue' is not assignable to type 'string[]'.
Type 'string' is not assignable to type 'string[]'.
我的设置文件如下:
{
...
"APPS_1": ["abc", "def", "ghi", "jkl"],
"APPS_2": ["mno", "pqr"],
...
}
console.log(this.electronService.settings.get('APPS_1')) 给出:
我无法理解为什么。有人可以给我一些相同的指示吗?
谢谢。
【问题讨论】:
-
能否请您发布设置数据及其外观
-
console.log(this.electronService.settings.get('APPS_1'))的输出? -
通常设置是键值对,右侧有文字。如果您已将 json 放入其中,则可能需要对其进行去字符串化。您也可以只解析分隔符(例如用空字符串替换 [ 字符串),然后在 ',' 上拆分字符串。
-
当然,我的意思是去字符串化 JSON.parse() 。所以你说 var jsonversion = JSON.parse(this.electronService.settings.get('APPS_1'));
-
不确定但试试 this.supportInformation.appsSet1 = this.electronService.settings.get('APPS_1');
标签: angular typescript electron angular6