【问题标题】:Ionic 2 Angular 2 getting REST JSON dataIonic 2 Angular 2 获取 REST JSON 数据
【发布时间】:2016-10-18 02:36:31
【问题描述】:

我可能只是在某个地方犯了一个愚蠢的错误,但我只是不再看到它了。

我有一个非常简单的 Ionic 2 Angular 2 应用程序。请不要打扰架构,我只是想让它先工作。我正在尝试使用从 REST JSON 服务获得的模块对象填充我的数组“configuredModules”。我创建了一个将数组记录到控制台的按钮,因此我可以看到数组的值是什么,但始终未定义。我做错了什么?

export class Module {
    type: string;
    physicalLocationName: string;
}

和我的主要课程:

import { Component, OnInit } from '@angular/core';
import { Module } from './module';
import { HttpModule, Http, Response} from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/toPromise';

import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage implements OnInit{
  configuredModules: Module[];
  http: Http;

  constructor(public navCtrl: NavController,private _http: Http) {
    this.http = _http;
  }

  getConfiguredModules(){
      this.getModules()
        .then(modules => this.configuredModules = modules);
        console.log('finished configuredModules');
  }

  getModules(): Promise<Module[]> {
    return       this.http.get('http://localhost:8080/rest/modules/configuredModules')
           .toPromise()
           .then(response => response.json().data as Module[])
          ;
  }

  ngOnInit() {
      this.getConfiguredModules();
  }

  buttonClicked(){
    console.log('button clicked');
    console.log(this.configuredModules.values);
  }
}

如果我查看 Chrome 的网络标签,我可以看到以下内容(响应中):

{"modules":[{"type":"myTestType","physicalLocationName":"myTestPhysicalLocation"}]}

所以我知道数据正在进入,但它并没有填满我的数组。有什么建议么? 我认为我的代码看起来与 Angular 2 的英雄教程相同: https://angular.io/docs/ts/latest/tutorial/toh-pt6.html

【问题讨论】:

  • 你试过 console.log(JSON.stringify(this.configuredModules)); ?
  • 究竟在哪里未定义?在按钮单击事件中?应该是console.log(this.configuredModules);
  • 我认为数组未定义?我还尝试在没有 .values 的情况下记录它,然后我只是不确定。如果我在上面的示例中使用 .values,它会显示:TypeError:无法读取未定义的属性“值”。这让我相信 configureModules 是未定义的。我试图 JSON.stringify 它,同样的结果: undefined
  • 什么是.values? AFAIK 没有什么比 .values 更好的了。只需按照我上述评论中的说明使用即可。
  • 啊,我的 IDE (Atom) 提供了它作为选项。我认为它会打印数组中的值。或者至少是什么。没有 .values 的打印给了我“未定义”

标签: angular ionic-framework ionic2


【解决方案1】:

你的错误在于这个函数:

getModules(): Promise<Module[]> {
  return this.http.get('http://localhost:8080/rest/modules/configuredModules')
    .toPromise()
    .then(response => response.json().data as Module[]);
}

删除json() 函数后的.data 并添加.modules。这就是来自您的 API 的内容,因此应该如下所示:

getModules(): Promise<Module[]> {
    return this.http.get('http://localhost:8080/rest/modules/configuredModules')
           .toPromise()
           .then(response => response.json().modules as Module[]);
  }

然后在你的按钮点击功能上,删除values,所以它看起来像这样:

buttonClicked(){
  console.log('button clicked');
  console.log(this.configuredModules);
}

【讨论】:

  • 就是这样!非常感谢法比奥!你知道为什么Angular 2(英雄教程)的教程设置不同吗?
  • 因为他们的 API 和你的不同。您的 API 返回带有 {"modules":[{"type":"myTestType","physicalLocationName":"myTestPhysicalLocation"}]} 的对象键,但 API 的良好做法告诉我们,我们应该始终将 API 响应包装在数据对象中,请查看 this
  • 非常感谢你,伙计!你是冠军!
猜你喜欢
  • 2017-03-08
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 2017-09-12
  • 1970-01-01
相关资源
最近更新 更多