Angular 7 JSON.stringify“;无法解析符号JSON”;
首先,这是我第一次使用Angular 7;我开始使用Angular 7和c#后端制作一个应用程序,在将对象发送到我的Angular 7 JSON.stringify“;无法解析符号JSON”;,json,angular,angular7,Json,Angular,Angular7,首先,这是我第一次使用Angular 7;我开始使用Angular 7和c#后端制作一个应用程序,在将对象发送到我的控制器/服务之前,我需要序列化组件/服务中的对象 比如: export class jsonTest { json: string; obj: myType = {} as myType; this.obj.someProperty = 1234; this.obj.anotherProperty = 'test'; someMetho
控制器/服务之前,我需要序列化组件/服务中的对象
比如:
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()
,它不会被识别为任何类型的钩子
我有没有遗漏什么重要的东西?查看角度文档并不能说明我的问题
此时,我正在考虑手动序列化JSON,但如果可以的话,我真的希望避免这样做。有什么建议吗?您的打字稿有一些错误。试着这样做
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);
}}
当您打开浏览器调试窗口并在控制台中键入JSON.stringify
时,输出是什么?您使用的是resharper吗?啊,它与angular无关:)我只是因为另一个答案才找到它的。你可以将你的问题作为它的副本关闭,然后其他人也会被重定向。问题实际上是在VS2016中Resharper抛出错误。这仅仅是一些psudo代码,用于示例目的:)无论如何,谢谢!