我想创建包含我的自定义类的类。
此处我的班级是客户,其中包含的自定义类是国家/地区。
对于国家,我有以下课程:
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
答案 0 :(得分:1)
一些事情。
→
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
}
→
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
}
);
}
→
this.country=new Country(...)