javascript - Angular 传递子数据以查看

标签 javascript angular typescript

我有一个从 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}}

返回数据

one

有什么想法吗?

更新

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/

相关文章:

angular - Graphql 与数组类型的变异

javascript - 使用 javascript rrule.min.js 实现 RRule

javascript - CORS 和 Origin header ?

javascript - 如何使 jQuery 代码不适用于已登录的 Wordpress 用户?

来自共享模块的 Angular 8 指令不起作用

Angular CLI - 错误蓝图无效 : guard

javascript - 如何使用 sequelize 和 sqlite 验证 int 和 boolean

knockout.js - Typescript 在内部模块中导入外部模块

javascript - 从下方顺利滑入新的 div

javascript - Promise.all() - 在触发多个事件监听器后执行某些操作