2017-07-20 23 views
0

動的ng2 search.serviceを作成しようとしています。 RESTサービスは、組織内のデータを検索するための基礎となるプロトコルです。ここでユーザーを検索し、私が最初に作成さuser.serviceだ:動的ng2 search.serviceを作成しようとしています

import { Injectable } from '@angular/core'; 
import { Headers, Http, Response } from '@angular/http'; 
import 'rxjs/add/operator/toPromise'; 
import { User } from '../entities/user'; 

@Injectable() 
export class SearchService 
{ 
    private _headers = new Headers({ 'Content-Type': 'application/json' }); 
    private _entityName = ''; 
    private _allEntitiesUrl = 'http://localhost:4000/@entityNames'; 
    private _entityByIdUrl = 'http://localhost:4000/@entityNames/@id'; 

    constructor(private http: Http, entityName: string) 
    { 
     this._allEntitiesUrl = entityName + 's'; 
     this._entityByIdUrl = entityName + 's'; 
    } 

    getEntity(id: number): Promise<User> 
    { 
     var url = this._entityByIdUrl.replace('@id', id.toString()); 
     return this.http.get(url) 
      .toPromise() 
      .then(response => response.json()) 
      .catch(this.handleError); 
    } 

    getEntities(): Promise<User[]> 
    { 
     return this.http.get(this._allEntitiesUrl) 
      .toPromise() 
      .then(response => response.json()) 
      .catch(this.handleError); 
    } 

    private extractData(response: Response) 
    { 
     return response.json(); 
    } 

    private handleError(error: any): Promise<any> 
    { 
     console.error('An error occurred', error); // for demo purposes only 
     return Promise.reject(error.message || error); 
    } 
} 

search.service今部分的にしか完了している:

import { Injectable } from '@angular/core'; 
import { Headers, Http, Response } from '@angular/http'; 
import 'rxjs/add/operator/toPromise'; 
import { User } from '../entities/user'; 

@Injectable() 
export class UserService { 

    private headers = new Headers({ 'Content-Type': 'application/json' }); 
    private usersUrl = 'http://localhost:4000/users'; 
    private userUrl = 'http://localhost:4000/users/@id'; 

    constructor(private http: Http) { } 

    getUser(id: number): Promise<User> { 
     return this.http.get('http://localhost:4000/users/1') 
      //return this.http.get(this.userUrl.replace('@id', id)) 
      .toPromise() 
      //.then(this.extractData) 
      .then(response => response.json()) 
      .catch(this.handleError); 
    } 

    getUsers(): Promise<User[]> { 
     return this.http.get(this.usersUrl) 
      .toPromise() 
      .then(response => response.json()) 
      .catch(this.handleError); 
    } 

    private extractData(response: Response) { 
     return response.json(); 
    } 

    private handleError(error: any): Promise<any> { 
     console.error('An error occurred', error); // for demo purposes only 
     return Promise.reject(error.message || error); 
    } 
} 

ここでは、一般的なsearch.serviceで私の最初の試みです。ここで私は知っているいくつかのものを更新する必要があります:

  1. import文は、一般的な作られたか、何らかの形で潜在的にsearch.serviceから返される可能性が実体の異なる種類を収容する必要があります。たとえば、search.serviceはアカウント、商品などを返す可能性があります。

    import {User} from '../entities/user';

  2. getEntity/getEntitiesの動的な戻り値の型を実装する方法を理解する必要があります。私は私が持っている最初の人ではないんだと確信している、過去にC#のジェネリック型を返すためのメソッドを実装するために、いくつかの仕事をしたが、これはNG2/typescriptです

で行われることになるかどうかはわかりませんしましたこれをやろうと思ったので、ng2/typescriptにもっと慣れ親しんでいる他の人たちが私がこれをどのように実装できるか説明できます。

答えて

0

import文は、一般的なものにするか、search.serviceから返される可能性のあるさまざまな種類のエンティティに対応する必要があります。あなたがinterfaceを作成し、別のアイテムがそのインターフェイスを実装することができ活字体で

:たとえば、search.serviceは潜在的にアカウント、製品、などを返すことができます。検索サービスは、あなたのタイプのインターフェイスを返すことができます。例えば

class User implements Entity 

およびサービスは、私がgetEntity/GETENTITIESのための動的な戻り値の型を実装する方法を把握する必要があり

getEntities(): Promise<Entity[]> 
{ 
    return this.http.get(this._allEntitiesUrl) 
     .toPromise() 
     .then(response => response.json()) 
     .catch(this.handleError); 
} 

だろう。私はあなたがここで言及として、他のオプションがあり、過去にC#のジェネリック型を返すためのメソッドを実装するために、いくつかの仕事をしたが、これはNG2/typescriptですとどのように行われるかわからない

ましgenerics例えばを使用することです

getEntities<User>() 
と呼ばれます

getEntities<T>(): Promise<T[]> 
{ 
    return this.http.get(this._allEntitiesUrl) 
     .toPromise() 
     .then(response => response.json()) 
     .catch(this.handleError); 
} 

関連する問題