我有如下类别:
class ExampleComponent {
application: Application = new Application();
company: Company;
bank_account: BankAccount = new BankAccount();
contact: Contact;
director: Director = new Director();
websites: Website[] = [];
files: DirectorFile[] = [];
director_files: DirectorFile[] = [];
ngOnInit() {
this.getApplication();
}
getApplication() {
forkJoin([
this.exampleRepo.getApplication(),
this.exampleRepo.getCompany(),
this.exampleRepo.getBankAccount(),
this.exampleRepo.getContact(),
this.exampleRepo.getDirector(),
this.exampleRepo.getAllWebsites(),
this.exampleRepo.getAllFiles(),
this.exampleRepo.getDirectorFiles()
]).subscribe(
(data: [
Application,
Company,
BankAccount,
Contact,
Director,
Website[],
DirectorFile[],
DirectorFile[]
]) => {
this.application = data[0];
this.company = data[1];
this.bank_account = data[2];
this.contact = data[3];
this.director = data[4];
this.websites = data[5];
this.files = data[6];
this.director_files = data[7];
}
);
}
}
我得到以下错误:
error TS2345: Argument of type '(data: [Application, Company, BankAccount, Contact, Director, Website[], DirectorFile[], DirectorFile[]]) => void' is not assignable to parameter of type '(value: (Observable<Application> | Observable<BankAccount> | Observable<Contact> | Observable<Company> | Observable<Director> | Observable<DirectorFile[]> | Observable<Website[]>)[]) => void'.
Types of parameters 'data' and 'value' are incompatible.
Type '(Observable<Application> | Observable<BankAccount> | Observable<Contact> | Observable<Company> | Observable<Director> | Observable<DirectorFile[]> | Observable<Website[]>)[]' is not assignable to type '[Application, Company, BankAccount, Contact, Director, Website[], DirectorFile[], DirectorFile[]]'.
Property '0' is missing in type '(Observable<Application> | Observable<BankAccount> | Observable<Contact> | Observable<Company> | Observable<Director> | Observable<DirectorFile[]> | Observable<Website[]>)[]'.
如果我不指定数据类型,只是写道:data => {...}
我得到以下错误:
error TS2322: Type 'Observable<Application> | Observable<BankAccount> | Observable<Contact> | Observable<Company> | Observable<Director> | Observable<DirectorFile[]> | Observable<Website[]>' is not assignable to type 'Application'.
Type 'Observable<Application>' has no properties in common with type 'Application'.
如果我删除从forkJoin
阵列,只是增加了8个请求作为单个参数,我能够将数据分配给变量,但后来我从棉短绒和rxjs说与resultSelector是forkjoin已过时的误差。
任何想法如何,我可以将数据分配给我的变量。如何破坏观测量的数组对象?
当您订阅forkjoin将返回数据数组你打电话,以避免并获得单一可观察你只需要破坏你的数据阵列和智能感知错误应该消失,你的错误被路过的数据,而不是destructable观察到:
forkJoin(
[
this.service.getProvince(),
this.service.getLingueStraniere(),
]).subscribe(
([Province, LingueStraniere]) => {
this.province = Province;
this.lingueStraniere = LingueStraniere;
}
);
现在,你的代码应该是这样的:
forkJoin([
this.exampleRepo.getApplication(),
this.exampleRepo.getCompany(),
this.exampleRepo.getBankAccount(),
this.exampleRepo.getContact(),
this.exampleRepo.getDirector(),
this.exampleRepo.getAllWebsites(),
this.exampleRepo.getAllFiles(),
this.exampleRepo.getDirectorFiles()
]).subscribe(
([
application,
company,
bankAccount,
contact,
director,
website[],
directorFile[],
]) => {
this.application = application;
this.company = company;
this.bank_account = bankAccount;
this.contact = contact;
this.director = director;
this.websites = website[];
this.files = directorFile[];
}
);
如果你不这样做叉之前指定单个请求的类型,接口也不会在你的服务在您定义应用程序的HTTP请求映射值,以他的工作界面,所以:
.pipe(map((value: Application) => value))
您需要将数据映射到接口,或叉加入会给你的错误,如果你不这样做,默认的类型将是任何会对比你在forkJoin定义接口类型。
在JavaScript对象的基本类型是功能。 在你的代码,你想分配“式的价值”,以“功能”。 对于assing纠正你service.ts返回Obserbable,型函数应该返回可观察<类型>。 例如:
getApplication(): Observable<Application> {}
而在认购后的函数数据阵列将是你的类型未指派的。
(data) => {
this.application = data[0];
...
}