使用服务器中请求的值来创建类的实例

时间:2017-12-10 11:47:01

标签: angular observable

我想创建包含我的自定义类的类。

此处我的班级是客户,其中包含的自定义类是国家/地区

对于国家,我有以下课程:

export class Country {
    constructor(public shortcut: string, public description: string) { }
}

我从国家/地区服务

中获取国家/地区的列表
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/map';

import { Country } from './country';

@Injectable()
export class CountryService {

  countries: Country[];

  constructor(private http: Http) {
    this.getCountries().subscribe();
   }

  getCountries(): Observable<any> {
    if(this.countries != null) {
      return Observable.of(this.countries)
    } else {
      return this.http.get(`http://my-json-server.typicode.com/kolomu/CustomerDemoApp/countries`, { method: 'Get'})
      .map( (response) => response.json() )
      .map( (countries) => this.countries = countries)
    }
  }

  getDescription(shortcut: string): Observable<string> {
    if(this.countries != null) {
      let tempCountry = this.countries.find( (e) => e.shortcut == shortcut);
      return Observable.of(tempCountry.description);
    } else {
      this.getCountries().subscribe(
        (countries) => {
          let tempCountry = this.countries.find( (e) => e.shortcut == shortcut);
          return Observable.of(tempCountry.description);
        }
      );
    }

  }

}

我最初的想法是,我有一个国家/地区服务,它始终包含国家/地区描述和国家/地区快捷方式,以便于访问。例如。调用getDescription(countryShortcut)来获取提供的国家/地区快捷方式的描述。然而,正如我从Observable那里学到的那样,事情不可能像我想象的那样。

要获得国家描述,我需要这样的电话:

this.countryService.getDescription.subscribe(
  countryDescription => // Here I have the return value from country description
)

我的问题是,不知何故,措辞和方法调用不再有意义。

请参阅下面的Customer类,了解如何在getDescription调用中创建国家/地区...

import { AppInjector } from './app-injector';
import { Country } from './country';
import { CountryService } from './country.service';

export class Customer {

    public country: Country;
    public countryService: CountryService;

    constructor(public name: string, public countryShortcut: string){
        this.countryService = AppInjector.get(CountryService);


        this.countryService.getDescription(countryShortcut).subscribe(
            countryDescription => {
                return new Country(countryShortcut, countryDescription)
            } 
        )

    }


}

我的思维错误在哪里?也许用我自己的国家实例创建客户的完整过程是错误的?任何帮助都很高兴。

以下是该项目的github回购。 https://github.com/kolomu/CustomerDemoApp

1 个答案:

答案 0 :(得分:1)

一些事情。

  1. 使用httpClient,而不是http。所以,你不需要使用response.json()
  2. 使用Observable&lt;任何[]&gt;如果要订阅返回数组的observable 3.-map改变响应。如果你想对响应使用do,那么
  3. getCountries(): Observable<any[]> { //<--return an array
        if(this.countries != null) {
          return Observable.of(this.countries);
        } else {
          return this.http.get(`http://my-json-server.typicode.com/kolomu/CustomerDemoApp/countries`)  //we don't need json because we use httpClient
                            //If use "get" not put "method get"
        .do((result)=>this.countries=result); //use "do" to do something more with the response. do NOT change the response
    }
    
    1. 您没有在方法getDescription中订阅。您使用“map”来更改响应。或switchMap
    2. getDescription(shortcut: string): Observable<string> {
          if(this.countries != null) {
            let tempCountry = this.countries.find( (e) => e.shortcut == shortcut);
            return Observable.of(tempCountry.description);
          } else {
            this.getCountries().map( //If not work try switchMap instead map
              (countries) => {
                let tempCountry = this.countries.find( (e) => e.shortcut == shortcut);
                return tempCountry.description; //not Observable.of
              }
            );
          }
      
      1. 您的类必须是构造函数
      2. 中的参数的私有
      3. 在您的客户中必须
      4. this.country=new Country(...)
        
        1. 在我看来,你不需要类,只需要接口,(你没有类中的方法)
相关问题