私は、クラスをインスタンス化し、クラスでサービスを利用しようとしていますで、クラスでサービスを利用することはできませんが、私はは活字体
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
プライベートhttp:typeof Http' < - なぜあなたはそれをしましたか? – jonrsharpe
OK編集済み、最新コードではありませんでした。問題 – iamwhitebox
を解決しようとしていましたが、ここでtypescriptのクラスを読むことができます:https://www.typescriptlang.org/docs/handbook/classes.html 、高度なテクニックセクションと*クラスインスタンスの型とクラスコンストラクタの型の区別* – CRice