我在代码中遇到两个错误,不知道如何解决。
import { Component, OnInit } from '@angular/core';
import { ApiService } from 'app/services/api/api.service';
import { map } from 'rxjs/operators';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { Router } from '@angular/router';
import * as moment from 'moment';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, Subscription } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-invites',
templateUrl: './invites.component.html',
styleUrls: ['./invites.component.scss'],
})
export class InvitesComponent implements OnInit {
clientData: Observable<any>;
data: any = [];
cities:Set = new Set();
url:any;
constructor(private api:ApiService,private modalService: NgbModal,private router:Router, public httpClient: HttpClient
) {
this.getClaims();
}
ngOnInit() {
}
userFilter: any = { city: '', name: ''};
getClaims(){
this.url = 'url.com';
this.clientData = this.httpClient.get<any>(this.url,{responseType: 'json'}).
subscribe(data => {
console.log(data);
this.data = data.records;
this.data.forEach(d => this.cities.add(d.city));
});
}
}
1)错误TS2322:无法将“预订”类型分配给“可观察”类型。 “预订”类型中缺少属性“ _isScalar”。 2)通用类型“集”需要1个类型参数。
答案 0 :(得分:1)
1)如错误消息中所述,您正在为this.http.get(...).subscribe()
类型的clientData分配一个预订Observable<any>
clientData: Subscription
在这里是正确的类型。
2)在打字稿中,您需要输入将传递给Set的参数:
cities: Set<type of your city here>
答案 1 :(得分:1)
我认为您在此处设置类型时有些困惑
在您的代码中,您是说clientData
是Observable<any>
,但是,当您调用getClaims
方法
getClaims() {
this.url = 'url.com';
// here is an observable
this.clientData = this.httpClient.get<any>(this.url, {responseType: 'json'});
// here it becomes a Subscription
this.clientData = this.clientData.subscribe(data => {
console.log(data);
this.data = data.records;
this.data.forEach(d => this.cities.add(d.city));
});
}
对于您的Set
,您需要一个类型提示,就像对Observable<any>
所做的那样,这样它将变为
cities: Set<another type here>
旁注:请覆盖您的API网址,因为它实际上显示了一些内容