Я изучаю угловой и пытался отобразить только userName
из данных JSON, которые отправляются через URL.Но он не возвращает конкретный userName
, который я хочу отобразить.
Ошибка: TypeError: Невозможно прочитать свойство 'userName' со значением 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);
});
}
}