我有一个从 API 返回用户数据的页面,用户数据有一个名为 store
的子数组,我无法将这个子数组的数据获取到我的 View 中,它返回 undefined
代码
export class TokoPage implements OnInit {
store= null;
token: any;
constructor(
private authService: AuthService,
private navCtrl: NavController,
private storage: NativeStorage,
) {
this.storage.getItem('token').then((token) => {
this.token = token;
}).catch(error => console.error(error));
}
ngOnInit() {
this.authService.user().subscribe((user) => {
console.log('auth user', user);
console.log('1', user['sotre']); //undefined
});
}
}
查看
{{store.name}}
返回数据
有什么想法吗?
更新
JSON.stringify(user)
结果
{
"success":[
{
"id":1,
"name":"xxxxx",
"username":"admin",
"email":"xxxxx",
"phone":"xxxxx",
"avatar":null,"
coins":0,
"type":"admin",
"email_verified_at":"2019-08-13 14:08:09",
"created_at":"2019-08-13 14:08:09",
"updated_at":"2019-08-13 14:08:09",
"products":[{xxxxxxx}],
"addresses":[],
"wishlist":[],
"orders":[],
"store":{
"id":1,
"name":"xxxxx",
"url":"xxxxx",
"logo":null,
"banner":null,
"description":"xxxxx",
"kota_id":xxxxx,
"province_id":xxxxx,
"province":"xxxxx",
"kota":"xxxxx",
"address":"xxxxx",
"phone":"xxxxx",
"user_id":1,
"active":"yes",
"verified":"yes",
"best_seller":"yes",
"created_at":"2019-08-13 14:08:09",
"updated_at":"2019-08-15 09:17:40"
},
"rating":[]
}
]
}
最佳答案
首先,你打错了
console.log('1', user['store']);
其次,你访问的方式不对,success
是一个数组
this.authService.user().subscribe((user: any) => {
console.log('auth user', user.success[0]['store']);
});
let user = {
"success": [{
"id": 1,
"name": "xxxxx",
"username": "admin",
"email": "xxxxx",
"phone": "xxxxx",
"avatar": "null",
"coins": 0,
"type": "admin",
"email_verified_at": "2019-08-13 14:08:09",
"created_at": "2019-08-13 14:08:09",
"updated_at": "2019-08-13 14:08:09",
"products": [],
"addresses": [],
"wishlist": [],
"orders": [],
"store": {
"id": 1,
"name": "xxxxx",
"url": "xxxxx",
"logo": null,
"banner": null,
"description": "xxxxx",
"kota_id": "xxxxx",
"province_id": "xxxxx",
"province": "xxxxx",
"kota": "xxxxx",
"address": "xxxxx",
"phone": "xxxxx",
"user_id": 1,
"active": "yes",
"verified": "yes",
"best_seller": "yes",
"created_at": "2019-08-13 14:08:09",
"updated_at": "2019-08-15 09:17:40"
},
"rating": []
}]
};
console.log(user.success[0]['store']);
关于javascript - Angular 传递子数据以查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57619525/