【问题标题】:Angular 7 JSON.stringify "symbol JSON cannot be resolved" [duplicate]Angular 7 JSON.stringify“无法解析符号 JSON”[重复]
【发布时间】:2018-12-13 23:06:32
【问题描述】:

首先,这是我第一次使用 Angular 7;我开始使用带有 c# 后端的 Angular 7 制作应用程序,并且需要先序列化我的 component/service 中的对象,然后再将其发送到我的 controller/service

类似:

export class jsonTest  {
    json: string;
    obj: myType = {} as myType;

    this.obj.someProperty = 1234;
    this.obj.anotherProperty = 'test';

    someMethod() {
        this.json = //convert obj to json
        anotherMethod(this.json);
    }
}

在寻找如何实现这一点的过程中,我遇到了两个流行的建议,一个是JSON.stringify(),另一个是toJson()

但是,JSON.stringify() 会引发 symbol JSON cannot be resolved, probably it is located in an inaccessible module. 的编译错误

尝试toJson(),它不会被识别为任何类型的钩子。

是否有一些我缺少的导入?浏览 Angular 文档并不能解决我的问题。

此时,我正在考虑手动序列化 JSON,但如果可以的话,我真的很想避免这样做。有什么建议吗?

【问题讨论】:

  • 当您打开浏览器调试窗口并在控制台中输入JSON.stringify 时,输出是什么?
  • 你在使用 resharper 吗?
  • 啊,和角度无关:)
  • 我只是因为另一个答案才找到它。您可以将您的问题作为其副本关闭,然后其他人也会被重定向。

标签: json angular angular7


【解决方案1】:

您的打字稿中有一些错误。尝试这样做。

export class JsonTest implements OnInit {
json: string;
obj: MyType = new MyType();

ngOnInit(): void {
    this.obj.someProperty = 1234;
    this.obj.anotherProperty = 'test';
}
someMethod() {
    this.json = JSON.stringify(this.obj);
    anotherMethod(this.json);
}}

【讨论】:

  • 问题实际上是 Resharper 在 VS2016 中抛出错误。无论如何,这只是一些用于示例目的的伪代码:) 无论如何谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 2019-03-26
  • 2017-04-24
  • 1970-01-01
相关资源
最近更新 更多