无法在Typescript中使用类中的服务

时间:2017-11-02 18:01:42

标签: angular typescript

我正在尝试在类中使用服务,然后实例化该类,但我一直在使用

bufferCount(2, 1)

但是当我将私有服务更改为typeof Argument of type 'typeof SearchService' is not assignable to parameter of type 'SearchService'. Property 'apiService' is missing in type 'typeof SearchService'. 时,我再也无法调用它的方法 - 如何在我的InitialDataset类中使用此服务?

api.service.ts

SearchService

search.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
    ) { }
...

初始数据集类:

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}`);
    }
}

1 个答案:

答案 0 :(得分:1)

您的课程采用IO (Maybe Double)实例

SearchService类本身不是实例,因此您会收到错误。

您需要使用SearchService或Angular injection创建实例。