Angular 2 Http Service Injectable

时间:2016-07-19 01:20:59

标签: http service angular injectable

当网站启动时,我正在使用Angular 2服务从我的服务器中提取一个大对象。我需要提取的数据如下:

{
    Edu: [...],
    Exp: [...],
    Links: [...],
    Portfolio: [...],
    Skills: [...]
}

我以这种方式设置服务:

AllDataService:

import { Injectable, OnInit } from "@angular/core";
import { Http, Response } from "@angular/http";
import { Observable } from "rxjs/Rx";

@Injectable()
export class AllDataService {
  private allDataUrl = ".../allData";
  private loading: boolean;
  private Edu: Array<any>;
  private Exp: Array<any>;
  private Links: Array<any>;
  private Portfolio: Array<any>;
  private Skills: Array<any>;

  constructor(private http: Http) {
    this.loading = true;
    this.Edu = [];
    this.Exp = [];
    this.Links = [];
    this.Portfolio = [];
    this.Skills = [];
  }

  ngOnInit() {
    this.getAllData();
  }

  // Get data from api, aka "Set" methods
  getAllData() {
    return this.http.get(this.allDataUrl)
             .subscribe(
               data => {
                this.Edu = data.Edu;
                this.Exp = data.Exp;
                this.Links = data.Links;
                this.Portfolio = data.Portfolio;
                this.Skills = data.Skills;
                this.loading = false;
              },
               err => console.error(err)
             );
  }

  // “Get” methods
  getLoading() { return this.loading; }
  getEdu() { return this.Edu; }
  getExp() { return this.Exp; }
  getLinks() { return this.Links; }
  getPortfolio() { return this.Portfolio; }
  getSkills() { return this.Skills; }
}

在我的组件中,我注入了服务以便我可以获取数据:

HomeIcons:

import { Component } from "@angular/core";
import { AllDataService } from "../allDataService";

@Component({
  selector: "home-icons",
  template: `
            <div class="home-icons-wrapper">
              <ul class="home-icons-ul no-select">
                <li class="home-icons-li"
                *ngFor="let link of links" >
                  <a href={{link.url}} target="_blank">
                    <span class="home-icons-icon {{link.icon}}"></span>
                  </a>
                </li>
              </ul>
            </div>
            `,
  providers: [AllDataService]
})

export class HomeIcons {
  public links;

  constructor(private http: Http, private allDataService: AllDataService) {
    this.links = allDataService.getLinks();
  }
}

但是,在AllDataService中,错误消息告诉我响应中不存在属性(Exp,Edu,Skills ...)。我应该如何正确设置我的http服务,以便我可以在启动时提取我想要的数据并确保所有组件都获取数据?感谢

1 个答案:

答案 0 :(得分:0)

您需要做的就是将您的回复转换为JavaScript对象:

// Get data from api, aka "Set" methods
getAllData() {
  return this.http.get(this.allDataUrl)
           .map(res => res.json()) // <-- this line here
           .subscribe(
             data => {
               this.Edu = data.Edu;
               this.Exp = data.Exp;
               this.Links = data.Links;
               this.Portfolio = data.Portfolio;
               this.Skills = data.Skills;
               this.loading = false;
             },
             err => console.error(err)
           );
}

直接在模板中绑定方法:

template: `
        <div class="home-icons-wrapper">
          <ul class="home-icons-ul no-select">
            <li class="home-icons-li"
            *ngFor="let link of allDataService.getLinks()" >
              <a href={{link.url}} target="_blank">
                <span class="home-icons-icon {{link.icon}}"></span>
              </a>
            </li>
          </ul>
        </div>
        `,