简体   繁体   English

消耗剩余离子3

[英]Consuming Rest Ionic3

I am new to the web and Ionic3 development and I have a problem, I followed a tutorial to consume a rest API, I was able to bring the list with the users, however I am wanting to bring a specific user and show it on screen if I change the URL for the user it shows me this error 我是Web和Ionic3开发的新手,我遇到了问题,我按照教程使用了剩余的API,可以将列表带给用户,但是我想带一个特定的用户并在屏幕上显示如果我更改用户的网址,则会显示此错误

TabCepPage.html:10 ERROR Error: Cannot find a differ supporting object
'[object Object]' of type 'Leanne Graham'. NgFor only supports binding 
to  Iterables such as Arrays.

my app is like this: 我的应用程序是这样的:

provider: rest.ts 提供者:rest.ts

apiUrl = 'https://jsonplaceholder.typicode.com';

constructor(public http: HttpClient) {
  console.log('Hello RestServiceProvider Provider');
}

getUsers() {
  return new Promise(resolve => {
    this.http.get(this.apiUrl+'/users/1').subscribe(data => {
      resolve(data);
    }, err => {
      console.log(err);
    });
  });
}

page .ts: .ts页:

users: any;

constructor(public navCtrl: NavController, public restProvider: RestProvider) {
  this.getUsers();
}

getUsers() {
  this.restProvider.getUsers()
  .then(data => {
    this.users = data;
    console.log(this.users);
  });
}

and so on HTML display: 等等在HTML显示中:

<ion-item *ngFor="let user of users">
  <h2>{{user.name}}</h2>
  <p>{{user.email}}</p>
</ion-item>

Can u help me? 你能帮我吗?

When trying to get an specific user on your code, this.users ends up being an object instead of an array which is what your template is trying to for loop. 当尝试在代码上获取特定用户时,this.users最终将成为对象而不是数组,这正是您的模板试图进行循环的对象。 You just need to update the template to display the object instead of trying to for loop it. 您只需要更新模板以显示对象,而不是尝试对其进行循环。

example:
<ion-item *ngIf="users">
  <h2>{{users.name}}</h2>
  <p>{{users.email}}</p>
</ion-item>

您如何看待该数组,可以使用console.log(JSON.stringify(variable name));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM