Saya menghadapi satu masalah saat bekerja di Angular 8.

File service.ts

 public getTopicsByContainerId(containerId) {
        var result;
        if (this.envUrl.accessMock == true) {
            result = topicsData;
            return of(result.filter(items => items.attributes.ContainerOid == containerId));
        }
        else {
            var url = this.getTopicsByContainerIdEndPoint.replace("{id}", containerId);
            return this.repository.getData(url).subscribe();
        }

    }

File component.ts

var output = this.homeService.getTopicsByContainerId(container_Oid).subscribe(res => {
   this.currentContainerTopics = res[0];
   });

Sekarang masalahnya adalah saya menghadapi kesalahan berikut saat memanggil layanan web dari file komponen.

ERROR in src/app/home/home.component.ts(98,72): error TS2339: Property 'subscribe' does not exist on type 'Subscription | Observable<any>'.
  Property 'subscribe' does not exist on type 'Subscription'.

Adakah yang bisa memberi tahu saya di mana saya melakukan kesalahan?

3
Jayesh Vyas 14 Januari 2020, 15:51

2 jawaban

Jawaban Terbaik

Anda perlu menambahkan tipe pengembalian Observable<YourType> ke metode Anda getTopicsByContainerId() dan menghapus metode subscribe di dalam metode getTopicsByContainerId():

public getTopicsByContainerId(containerId) : Observable<YourType> {
    var result;
    if (this.envUrl.accessMock == true) {
        result = topicsData;
        return of(result.filter(items => items.attributes.ContainerOid == containerId));
    }
    else {
        var url = this.getTopicsByContainerIdEndPoint.replace("{id}", containerId);
        return this.repository.getData(url);
    }

}
1
StepUp 14 Januari 2020, 12:55

Hapus .subscribe()

public getTopicsByContainerId(containerId) {
        var result;
        if (this.envUrl.accessMock == true) {
            result = topicsData;
            return of(result.filter(items => items.attributes.ContainerOid == containerId));
        }
        else {
            var url = this.getTopicsByContainerIdEndPoint.replace("{id}", containerId);
            // return this.repository.getData(url).subscribe();   // remove this line
            return this.repository.getData(url);   // this method should return Observable response
        }

    }
3
Jameer Khan 14 Januari 2020, 13:01