下面的代码从数据库中获取两个值。
这是通过使用我从未使用过的forkJoin
完成的。使用它的原因是因为我有一个函数,在调用函数this.complexWordIdentification(this.postIWant, this.theHardWords);
因为它在ngOnInit上,所以它一开始就加载它,这是我想要的,因为在这种情况下,我想从数据库中加载特殊词并从数据库中加载文本,然后通过一个函数运行它在开始时,该函数需要具有以下两个值:
this.theHardWords
和this.PostIWant
。
如下Observable.forkJoin(
所示,它不会到达console.log('first');
。谁能发现我在做什么错?谢谢!
ngOnInit() {
this.isLoading = true;
this.form = new FormGroup({
annotation: new FormControl(null, {
validators: [
Validators.required,
Validators.minLength(8),
Validators.maxLength(250)
]
})
});
this.id = this.route.snapshot.paramMap.get('postId');
this.annotationService.getWords();
this.postsService.getPosts();
Observable.forkJoin(
this.annotationService.getWordUpdateListener(),
this.postsService.getPostUpdateListener()
).subscribe(
data => {
// data[0] result from getWordUpdateListener
this.thewords = data[0];
this.thewords.map(word => {
console.log('first');
this.theHardWords.push(word.word);
this.wordWithAnnotation.push(word);
});
// data[1] result from getPostUpdateListener
this.posts.map(post => {
console.log('second');
if (post.id === this.id) {
this.postIWant = post.fileText;
}
});
console.log('third');
this.isLoading = false;
this.complexWordIdentification(this.postIWant, this.theHardWords);
},
err => {
console.log(err);
// error handling
}
);
this.role = this.authService.getUserRole();
this.userIsAuthenticated = this.authService.getIsAuth();
this.authStatus = this.authService
.getAuthStatus()
.subscribe(isAuthenticated => {
this.userIsAuthenticated = isAuthenticated;
this.role = this.authService.getUserRole();
});
this.isLoading = false;
}
如果您想提出任何问题,请随时回答。我想补充一点,我不太有经验...谢谢!
答案 0 :(得分:1)
尝试使用类似这样的东西。
forkJoin(
this.annotationService.getWordUpdateListener(),
this.postsService.getPostUpdateListener()
).subscribe(
// Your code
);
检查forkJoin的rxjs documentation。 我创建了一个小示例gist.github。