Angular 角度类型错误:类型';字符串';不可分配给类型';字符串[]和#x27;

Angular 角度类型错误:类型';字符串';不可分配给类型';字符串[]和#x27;,angular,typescript,electron,angular6,Angular,Typescript,Electron,Angular6,我是angular的新手,正在尝试利用angular 6构建一个电子应用程序 我想做的是: 1.SupportInformationClass有几个定义 2.在componenet的init上,从electron设置填充定义 支持信息。ts: export class SupportInformation { appsSet1: string[]; //appsSet2: string[]; //appsSet3: string[]; //appsSet4:

我是angular的新手,正在尝试利用angular 6构建一个电子应用程序

我想做的是: 1.SupportInformationClass有几个定义 2.在componenet的init上,从electron设置填充定义

支持信息。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
        }
    }
}
export class SupportInformation  {
    appsSet1: string[];
}
import { SupportInformation } from './supportInformation';
...
...

export class ConfigurationComponent implements OnInit {

  supportInformation: SupportInformation = {
        appSet1: []
  };
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.appSet1 = <[string]>this.electronService.settings.get('APPS_1');
        }
        console.log("this prints what I need: ", this.supportInformation);

    }

}
即使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'))提供:

我不明白为什么。有人能给我一些相同的建议吗


谢谢。

JSON。解析您的响应,然后分配。
试试这个,我希望它能起作用

JSON。解析您的响应,然后分配。
试试这个,我希望它能起作用

您需要使用JSON.parse将字符串转换为JSON对象:

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 = JSON.parse(this.electronService.settings.get('APPS_1')); // this gives an error
        }
    }
}

您需要使用JSON.parse将字符串转换为JSON对象:

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 = JSON.parse(this.electronService.settings.get('APPS_1')); // this gives an error
        }
    }
}

好的,根据@codeSetter-
this.electronService.settings.get('APPS_1')
留下的线索,在官方的帮助下,下面的工作似乎很好。现在唯一的问题是

实施情况如下:

支持信息。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
        }
    }
}
export class SupportInformation  {
    appsSet1: string[];
}
import { SupportInformation } from './supportInformation';
...
...

export class ConfigurationComponent implements OnInit {

  supportInformation: SupportInformation = {
        appSet1: []
  };
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.appSet1 = <[string]>this.electronService.settings.get('APPS_1');
        }
        console.log("this prints what I need: ", this.supportInformation);

    }

}
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
        }
    }
}
export class SupportInformation  {
    appsSet1: string[];
}
import { SupportInformation } from './supportInformation';
...
...

export class ConfigurationComponent implements OnInit {

  supportInformation: SupportInformation = {
        appSet1: []
  };
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.appSet1 = <[string]>this.electronService.settings.get('APPS_1');
        }
        console.log("this prints what I need: ", this.supportInformation);

    }

}
从“/SupportInformation”导入{SupportInformation};
...
...
导出类ConfigurationComponent实现OnInit{
支持信息:支持信息={
appSet1:[]
};
构造函数(私有childProcessService:childProcessService,私有electronService:electronService){
log(“内部构造函数…”)
}
恩戈尼尼特(){
console.log(“在ngInit上…”);
这个.getSupportedApps();
}
getSupportedApps(){
if(this.childProcessService.isElectronApp){
this.supportInformation.appSet1=this.electronService.settings.get('APPS_1');
}
log(“这打印我需要的:”,this.supportInformation);
}
}

好的,根据@codeSetter-
this.electronService.settings.get('APPS_1')
留下的线索,在官方的帮助下,下面的工作似乎很好。现在唯一的问题是

实施情况如下:

支持信息。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
        }
    }
}
export class SupportInformation  {
    appsSet1: string[];
}
import { SupportInformation } from './supportInformation';
...
...

export class ConfigurationComponent implements OnInit {

  supportInformation: SupportInformation = {
        appSet1: []
  };
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.appSet1 = <[string]>this.electronService.settings.get('APPS_1');
        }
        console.log("this prints what I need: ", this.supportInformation);

    }

}
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
        }
    }
}
export class SupportInformation  {
    appsSet1: string[];
}
import { SupportInformation } from './supportInformation';
...
...

export class ConfigurationComponent implements OnInit {

  supportInformation: SupportInformation = {
        appSet1: []
  };
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.appSet1 = <[string]>this.electronService.settings.get('APPS_1');
        }
        console.log("this prints what I need: ", this.supportInformation);

    }

}
从“/SupportInformation”导入{SupportInformation};
...
...
导出类ConfigurationComponent实现OnInit{
支持信息:支持信息={
appSet1:[]
};
构造函数(私有childProcessService:childProcessService,私有electronService:electronService){
log(“内部构造函数…”)
}
恩戈尼尼特(){
console.log(“在ngInit上…”);
这个.getSupportedApps();
}
getSupportedApps(){
if(this.childProcessService.isElectronApp){
this.supportInformation.appSet1=this.electronService.settings.get('APPS_1');
}
log(“这打印我需要的:”,this.supportInformation);
}
}

您可以发布设置的数据以及它在
console.log(this.electronService.settings.get('APPS_1'))中的显示方式吗?
通常设置是键值对,右边有一个文本。如果你把json放在那里,也许它需要被分解。您也可以只解析出分隔符(例如字符串替换[使用空字符串],然后在“,”上拆分字符串。当然,通过解字符串,我指的是JSON.parse()。因此您可以说var jsonversion=JSON.parse(this.electronService.settings.get('APPS_1');不确定,但请尝试这个。supportInformation.appsSet1=this.electronService.settings.get('APPS_1');您可以发布设置的数据以及它在控制台.log(this.electronService.settings.get('APPS_1'))中的外观输出数据吗??通常设置是键值对,右边有一个文本。如果您将json放入其中,可能需要对其进行分解。您也可以只解析出分隔符(字符串替换[例如,使用空字符串),然后在“,”上拆分字符串。当然,通过解字符串,我指的是JSON.parse()。所以你说var jsonversion=JSON.parse(this.electronService.settings.get('APPS_1'));不确定,但试试这个。supportInformation.appsSet1=this.electronService.settings.get('APPS_1'));嗨,杰夫-我不理解
JSON.parse(),JSON.stringify()
点。当我执行
console.log(this.electronService.settings.get('APPS_1'))
时,它已经是JS数组格式-正在执行
JSON.parse()
-查看OP中的屏幕截图,这会导致进一步的错误。不管怎样,我碰巧让它工作了,并回答了我自己的问题。就在此时,我正试图找出它工作的原因。:)嗨,杰夫-我不理解
JSON.parse(),JSON.stringify()
点。当我做
console.log时(this.electronService.settings.get('APPS_1'))
,它已经是一个JS数组格式-正在执行一个
JSON.parse()
-根据操作中的屏幕截图,这会导致进一步的错误。不管怎样,我碰巧让它工作了,并回答了我自己的问题。就在此时,我正试图找出它工作的原因。:)this.electronService.settings.get('APPS_1');这样做是将JSON键入字符串数组。this.electronService.settings.get('APPS_1');这样做是将JSON键入字符串数组。