나는 다음 클래스를 인스턴스화하는 클래스의 서비스를 사용하는 것을 시도하고있다 클래스에서 서비스를 사용할 수 없습니다,하지만 난는 타이프
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
'개인 http : typeof Http' <- 왜 그렇게 했습니까? – jonrsharpe
ok가 수정되었습니다. 최신 코드가 아닙니다. – iamwhitebox
OP 문제를 해결하려고 시도 했으므로 여기에 typescript의 클래스를 읽어 보시기 바랍니다. https://www.typescriptlang.org/docs/handbook/classes.html, 특히 , 고급 기술 섹션 및 클래스 인스턴스 유형과 클래스 생성자 유형의 구분 * – CRice