繁体   English   中英

打字稿 - 解析错误:预期表达

[英]Typescript - Parsing error: Expression expected

我正在使用 vue 和 typescript,但遇到问题,如何解决? 这是我的代码:

private setTitle(systemConfig: any) {
    const systemConfigParse;
    let obj;
    systemConfigParse = JSON.parse(systemConfig);
    obj = (<any>systemConfigParse).find((item: any) => {
      return item.Code == "hospitalName";
    });
    this.hospitalName = obj.Value;
    obj = (<any>systemConfigParse).find((item: any) => {
      return item.Code == "systemName";
    });
    this.systemName = obj.Value;
    this.title = this.hospitalName + this.systemName;
  }

错误在这一行return item.Code == "hospitalName"; 但是在我删除代码之后:

 obj = (<any>systemConfigParse).find((item: any) => {
          return item.Code == "hospitalName";
        });
 obj = (<any>systemConfigParse).find((item: any) => {
          return item.Code == "systemName";
        });

还是报错 是eslint的伎俩吗? 如何解决? 非常感谢

您可以大大简化您的代码:

public setTitle(systemConfig: string) {
    const systemConfigParse = JSON.parse(systemConfig);
    this.hospitalName = systemConfigParse.hospitalName;
    this.systemName = systemConfigParse.systemName;
    this.title = this.hospitalName + this.systemName;
}

数据的形状如下:

const jsonMock = `{"hospitalName":"hospital", "systemName": "system"}`;
yourInstance.setTitle(jsonMock);

我修好了它

private setTitle(systemConfig: any) {
    const systemConfigParse= JSON.parse(systemConfig);
    let obj;
    for(const item in systemConfigParse){
      const i = systemConfigParse[item];
      if(i.Code=="hospitalName"){
        this.hospitalName = i.Value;
      } else if(i.Code=="systemName"){
        this.systemName = i.Value;
      }
    }
    this.title = this.hospitalName + this.systemName;
  }

感谢@devdgehog,我使用forof代替forin

private setTitle(systemConfig: any) {
    const systemConfigParse = JSON.parse(systemConfig);
    let obj;
    for (const item of systemConfigParse) {
      if (item.Code == "hospitalName") {
        this.hospitalName = item.Value;
        console.log(this.hospitalName);
      } else if (item.Code == "systemName") {
        this.systemName = item.Value;
      }
    }
    this.title = this.hospitalName + this.systemName;
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM