如果我检查 RxJS 订阅方法,我可以看到: subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription;所以我写了这样的示例初始化函数: private init(): void{this.dataBaseService.fetchPersons().subscribe( (persons: Person[]) => { this.behaviorSubject.next(persons); this.subject.next(persons); }, error => console.error(error), () => console.log('Complete!'));}Typescript 是否需要为参数提供 lambda 函数?我可以在其他地方创建函数并将其作为参数提供吗?例如这个功能: (persons: Person[]) => { this.behaviorSubject.next(persons); this.subject.next(persons); }在上层创建,然后将其作为参数提供。好的,所以我尝试在类中创建一个方法: someFunction( persons: Person[] ){this.behaviorSubject.next(persons);this.subject.next(persons);}并试图将它传递给 init 函数 private init2(): void { this.dataBaseService.fetchPersons().subscribe( this.someFunction(), error => void, () => console.log('Complete!'); )}我收到错误:An argument for 'persons' was not provided.如果它初始化这个上层方法,我必须提供什么样的论据?
1 回答
长风秋雁
TA贡献1757条经验 获得超7个赞
你需要在没有的情况下传递你的函数,()否则它会立即被调用:
someFunction( persons: Person[]) {
this.behaviorSubject.next(persons);
this.subject.next(persons);
}
private init2(): void {
this.dataBaseService.fetchPersons().subscribe(
this.someFunction, // <- pass it without ()
error => void,
() => console.log('Complete!')
)
}
添加回答
举报
0/150
提交
取消
