2017-11-02 19 views
-2

私は、クラスをインスタンス化し、クラスでサービスを利用しようとしていますで、クラスでサービスを利用することはできませんが、私はは活字体

Argument of type 'typeof SearchService' is not assignable to parameter of type 'SearchService'. 
    Property 'apiService' is missing in type 'typeof SearchService'. 

を得続けるが、私は民間のサービスを変更する場合SearchServiceをtypeof演算し、I私のInitialDatasetクラス内でこのサービスをどのように使うことができるのですか?

api.service.ts

import { Injectable } from '@angular/core'; 
import { environment } from '../../../environments/environment'; 
import { Headers, Http, Response, URLSearchParams } from '@angular/http'; 
import { Observable } from 'rxjs/Rx'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/catch'; 

@Injectable() 
export class ApiService { 
    constructor(
     private http: Http 
    ) { } 
... 

search.service.ts

import { Injectable } from '@angular/core'; 
import { Observable } from 'rxjs/Rx'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/catch'; 

import { ApiService } from './api.service'; 
import { AssetList } from '../models'; 

@Injectable() 
export class SearchService { 
    constructor(
     private apiService: ApiService 
    ) { } 

    getInitialCollections(): Observable<AssetList> { 
     return this.apiService.post('/cmt-api/search/merch/collections/_search'); 
    } 

    getCollections(searchText: string | null): Observable<AssetList> { 
     return this.apiService.post(`/cmt-api/search/merch/collections/_search?searchText=${searchText}`); 
    } 
} 

初期データセットクラス:

export class InitialDataset { 

    /** Stream that emits whenever the data has been modified. */ 
    dataChange: BehaviorSubject<Asset[]> = new BehaviorSubject<Asset[]>([]); 
    get data(): Asset[] { return this.dataChange.value; } 

    constructor(private searchService: SearchService) { 
     // Fill up the dataset with initialData. 
     this.addAssets(); 
    } 

    /** Adds a new asset to the dataset. */ 
    addAssets() { 
     this.searchService.getInitialCollections() 
      .subscribe(assets => { 
       let results = assets.data 
       results.map(asset => this.dataChange.next(asset)); 
      }, err => { 
       console.log(err) 
      }); 
    } 
} 



exampleDatabase = new InitialDataset(SearchService); // Error 
+2

プライベートhttp:typeof Http' < - なぜあなたはそれをしましたか? – jonrsharpe

+0

OK編集済み、最新コードではありませんでした。問題 – iamwhitebox

+0

を解決しようとしていましたが、ここでtypescriptのクラスを読むことができます:https://www.typescriptlang.org/docs/handbook/classes.html 、高度なテクニックセクションと*クラスインスタンスの型とクラスコンストラクタの型の区別* – CRice

答えて

1

あなたのクラスは、SearchServiceインスタンスをとります。

SearchServiceクラス自体はインスタンスではないため、エラーが発生します。

newまたは角度注入を使用してインスタンスを作成する必要があります。

+0

Angular @Injectは、新しいInitialDataset(新しいSearchService())を渡そうとするときに役立ちます。 - SearchService import SearchService – iamwhitebox

+0

基本的に、これは次のようになります。new SearchService(new ApiService(new ApiService(ApiService:ApiService))新しいHttp(??????))))); – iamwhitebox

+0

これを避けるには? – iamwhitebox