json - Angular 7 JSON.stringify“无法解析符号 JSON”
问题描述
首先,这是我第一次使用 Angular 7;我开始使用带有 ac# 后端的 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);
}
}
在寻找如何实现这一点的过程中,我遇到了两个流行的建议,一个是being JSON.stringify()
,另一个是being toJson()
。
但是,JSON.stringify()
会引发编译错误symbol JSON cannot be resolved, probably it is located in an inaccessible module.
尝试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);
}}
推荐阅读
- performance - 站点在第一次加载非常慢的 IIS 调试
- neo4j - 是否可以在 CYPHER 中设置(复制)另一个节点的标签?
- reactjs - 在 React Hooks 中使用函数 useEffect() 的问题
- java - 如何从节点获取列表并在 recyclerView 中使用该列表从 firebase 获取数据
- javascript - 使用对象键值对动态创建变量?
- android - 我可以将 Firebase 更新通知发送到 Playstore 上早期没有 Firebase 连接的旧版应用程序吗?
- python - Python 烧瓶 - request.data.get
- assembly - nasm中的macro vs equ
- python - NameError:名称“xxxx”未定义
- java - 第一个文字游戏:打印行复制