json - 通过 url 获取 queryparams (JSON) - Angular 5
问题描述
我正在学习角度,并试图仅显示userName
通过 URL 发送的 JSON 数据中的数据。但它没有返回userName
我想要显示的具体内容。
错误是: TypeError:无法读取属性 'userName' of null
链接示例: http://10.20.30.40/abc?userInfo= hereIsTheJSON
JSON:
{
"company":[
{
"A":false,
"B":4321,
"C":null,
"D":"TASTE",
"E":1,
"F":null
},
{
"A":true,
"B":1234,
"C":null,
"D":"TEST",
"E":0,
"F":null
}
],
"A":false,
"C":null,
"E":0,
"F":null,
"userName":"Ron Marks",
"userId":"rMarks"
}
.TS:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
currentURL: string;
newArr:any[];
newObj: any;
myparams: any;
constructor(private route: ActivatedRoute,
) {
}
ngOnInit() {
this.route.queryParams.subscribe(queryParams => {
let userInfo = this.route.snapshot.queryParamMap.get('userInfo');
this.newObj = JSON.parse(userInfo);
console.log(this.newObj.userName);
});
}
}
解决方案
推荐阅读
- c# - C#中简单的口袋妖怪战斗的麻烦
- symfony4 - 子文件夹中的 Symfony 4 实体
- html - 如何对文本同时使用文本阴影和线性渐变?
- python - 为什么应用内核掩码得到高斯图像时,输出只是没有内容的白色图像?
- google-chrome-extension - chrome.storage.sync.get 总是返回默认值
- sql-server - SQL Server Management Studio 中的垂直选项卡 - 并排查询和结果?
- node.js - 我可以使用 MongoDB 模式模型来定义 IndexedDB 索引吗?
- javascript - 使用 onclick 事件传递参数
- sql - 如何获得在 CTE 中生成的随机数不会在 JOIN 中更改?
- html - css媒体不工作,是什么问题?