代码之家  ›  专栏  ›  技术社区  ›  expenguin

角度7JSON.stringify文件“symbol JSON无法解析”[重复]

  •  1
  • expenguin  · 技术社区  · 6 年前

    首先,这是我第一次使用Angular 7;我开始使用Angular 7制作一个带有c#后端的应用程序,并且需要在我的应用程序中序列化一个对象 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() ,它不被认为是任何类型的钩子。

    有什么我不知道的吗?翻阅角度文档并不能说明我的问题。

    在这一点上,我正在考虑手动序列化JSON,但是如果可以的话,我真的希望避免这样做。有什么建议吗?

    1 回复  |  直到 6 年前
        1
  •  -1
  •   Paul Hebert    6 年前

    你的打字稿有些错误。试着这样做。

    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);
    }}
    
    推荐文章