<分区>
首先,这是我第一次使用 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 无法解析,可能它位于一个无法访问的模块中。
尝试 toJson()
,它没有被识别为任何类型的钩子(Hook)。
我是否遗漏了一些重要信息?查看 Angular 文档并不能说明我的问题。
目前,我正在考虑仅手动序列化 JSON,但如果可以的话,我真的很想避免这样做。有什么建议吗?