【问题标题】:Cannot find name 'JSON' typescript file找不到名称“JSON”打字稿文件
【发布时间】:2020-06-25 03:12:45
【问题描述】:

我创建了一个 Angular 应用程序,我在其中执行 JSON 转换功能,例如 JSON.stringify、JSON.parse 我收到错误,例如找不到名称“JSON”,并且我在 tsconfig.json 中收到错误,如下所示

“--lib”选项的参数必须是:“es5”、“es6”、“es2015”、“es7”、“es2016”、“es2017”、“esnext”、“dom”、“dom.iterable” '、'webworker'、'scripthost'、'es2015.core'、'es2015.collection'、'es2015.generator'、'es2015.iterable'、'es2015.promise'、'es2015.proxy'、'es2015.reflect '、'es2015.symbol'、'es2015.symbol.wellknown'、'es2016.array.include'、'es2017.object'、'es2017.sharedmemory'、'es2017.string'、'es2017.intl'、'esnext .asynciterable'。

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,    
    "target": "es2015",   
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

login.component.ts

 Savesresponse(socialusers: Socialusers) {  
    this.SocialloginService.Savesresponse(socialusers).subscribe((res: any) => {  
      debugger;  
      console.log(res);  
      this.socialusers=res;  
      this.response = res.userDetail;  
      localStorage.setItem('socialusers', JSON.stringify( this.socialusers));  
      console.log(localStorage.setItem('socialusers', JSON.stringify(this.socialusers)));  
      this.router.navigate([`/Dashboard`]);  
    })  
  } 

【问题讨论】:

  • 看起来它拒绝了 ES2018 并且没有进入“dom”。你用的是什么版本的打字稿?

标签: json angular typescript


【解决方案1】:

在compilerOptions的lib中添加es5

  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "lib": [
      "es5",
      "dom"
    ]
  }

【讨论】:

  • 如果 OP 想要 ES2018 怎么办?
  • 目标入口点“angular-6-social-login”中的@prince 错误缺少依赖项:-@angular/core-@angular/common-rxjs
  • 你能不能试试把它从es5改成esnext或者es6
猜你喜欢
  • 2018-01-25
  • 2016-09-15
  • 2019-02-25
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 2017-06-18
  • 2018-11-28
  • 1970-01-01
相关资源
最近更新 更多