【问题标题】:JSON not found in Typescript application在 Typescript 应用程序中找不到 JSON
【发布时间】:2021-11-27 11:05:18
【问题描述】:

我有一个简单的采购类:

export class Purchase {

    customer!: Customer;
    
    shippingAddress!: Address;

    billingAddress!: Address;

    order!: Order;

    orderItems!: OrderItem[];
    
}

我有一个结帐组件,它执行以下操作:

// populate purchase - customer
purchase.customer = this.checkoutFormGroup.controls['customer'].value;

// populate purchase - shipping address
purchase.shippingAddress = this.checkoutFormGroup.controls['shippingAddress'].value;
const shippingState: State = JSON.parse(JSON.stringify(purchase.shippingAddress.state));
const shippingCountry: Country = JSON.parse(JSON.stringify(purchase.shippingAddress.country));
purchase.shippingAddress.state = shippingState.name;
purchase.shippingAddress.country = shippingCountry.name;

在此下面的行给出错误Cannot find name 'JSON'.ts(2304)

const shippingState: State = JSON.parse(JSON.stringify(purchase.shippingAddress.state));
const shippingCountry: Country = JSON.parse(JSON.stringify(purchase.shippingAddress.country));

我怀疑问题是我在tsconfig.json 中有以下配置:

"compilerOptions": {
    ...
    "target": "es2017",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
}

我尝试将es2018 更改为es5,但这并没有解决我的问题。无论哪种方式,肯定应该有一种方法可以用es2018 做这种事情。谁能告诉我如何解决这个问题?

【问题讨论】:

  • 你在 tsconfig.json 中试过这个吗? "compilerOptions": { "target": "es5", "module": "es2015", "lib": [ "es5", "dom" ] }
  • 是的。好像没什么区别。

标签: javascript angular typescript ecmascript-5 ecmascript-2018


【解决方案1】:

This 帮助我解决了我的问题。版本是 4.5.something。现在是 4.2.2,现在可以找到 JSON。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 2021-11-25
    • 1970-01-01
    • 2020-04-26
    • 2015-08-23
    • 2021-05-04
    • 1970-01-01
    相关资源
    最近更新 更多