2017-11-02 1 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, 나는 더 이상 그 메소드를 호출 할 수 없습니다.이 서비스를 내 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

OP 문제를 해결하려고 시도 했으므로 여기에 typescript의 클래스를 읽어 보시기 바랍니다. https://www.typescriptlang.org/docs/handbook/classes.html, 특히 , 고급 기술 섹션 및 클래스 인스턴스 유형과 클래스 생성자 유형의 구분 * – CRice

답변

1

귀하의 클래스는 예를 SearchService 걸립니다.

SearchService 클래스 자체는 인스턴스가 아니므로 오류가 발생합니다.

new 또는 각도 주입을 사용하여 인스턴스를 만들어야합니다.

+0

Angular @Inject는 새로운 InitialDataset (새로운 SearchService())을 전달하려고 할 때 도움이됩니다. - 오류가 발생합니다. 인수가 1 개이지만 예상 인수가 1 개 있습니다. (별칭) 새 SearchService (apiService : ApiService) : SearchService import SearchService – iamwhitebox

+0

기본적으로 다음과 같이 표시됩니다. new InitialDataset (new SearchService (new ApiService 새로운 Http (??????))))); – iamwhitebox

+0

어떻게 피 하시겠습니까? – iamwhitebox