可观察的 forkJoin 从不执行管道
Observable forkJoin never execute pipe
我正在尝试使用管道从可观察对象数组(每个对象都从 literalsService
获得)执行并获取值。这是代码:
translateLiterals() {
const literalsToTranslate: string[] = [
'certificate_title',
'with_address',
'hereby',
'declare',
'electronic_records',
'proceeded_to_shipment',
'return_address',
'return_address',
'addressee',
'message_subject',
];
const newArray: Observable<string>[] = [];
literalsToTranslate.map(literal => newArray.push(this.literalsService.getValue('Ngwa.Ngwa', literal)));
this.literalsArray$ = forkJoin([...newArray]).pipe(map((results) => {
console.log(results);
return results;
}));
}
当然,我订阅了 html 一侧的管道:
<ng-container *ngIf="literalsArray$ | async"></ng-container>
但是 console.log()
从来不打印任何东西...有人知道为什么吗?
可能是 this.literalsService.getValue('Ngwa.Ngwa', literal);
returns 字符串。
如果是这种情况,您需要将其转换为 Observable:
of(this.literalsService.getValue('Ngwa.Ngwa', literal));
同时 forkJoin
等待所有给定的流完成,然后发出值。
正如用户@ShamPooSham 和@martin 在评论中所说,问题是方法 this.literalsService.getValue ('Ngwa.Ngwa', literals)
正在观察文字的变化,而方法 forkJoin
从未启动,因为文字不是完全的。
我创建了一个单独的方法来解决问题,并在初始方法中添加了一个 take
:
新方法:
translateString(text: string): Observable<string> {
return this.literalsService.getValue('Ngwa.Ngwa', text).pipe(
switchMap((translated) => {
if (!translated.startsWith('N/A')) {
return of(translated);
} else {
return this.literalsService.getValue('Ngwa.Ngwa', text).pipe(
skip(1), // First event translation comes not translated
map((secondTryTranslation) => {
return secondTryTranslation;
}));
}
}),
);
}
第一种方法更正:
translateLiterals() {
const literalsToTranslate: string[] = [
'certificate_title',
'with_address',
'hereby',
'declare',
'electronic_records',
'proceeded_to_shipment',
'return_address',
'return_address',
'addressee',
'message_subject',
];
const newArray: Observable<string>[] = [];
literalsToTranslate.map(literal => newArray.push(this.translateString(literal).pipe(take(1)))); //<--- Here is the correction
this.literalsArray$ = forkJoin([...newArray]).pipe(map((results) => {
return results;
}));
}```
我正在尝试使用管道从可观察对象数组(每个对象都从 literalsService
获得)执行并获取值。这是代码:
translateLiterals() {
const literalsToTranslate: string[] = [
'certificate_title',
'with_address',
'hereby',
'declare',
'electronic_records',
'proceeded_to_shipment',
'return_address',
'return_address',
'addressee',
'message_subject',
];
const newArray: Observable<string>[] = [];
literalsToTranslate.map(literal => newArray.push(this.literalsService.getValue('Ngwa.Ngwa', literal)));
this.literalsArray$ = forkJoin([...newArray]).pipe(map((results) => {
console.log(results);
return results;
}));
}
当然,我订阅了 html 一侧的管道:
<ng-container *ngIf="literalsArray$ | async"></ng-container>
但是 console.log()
从来不打印任何东西...有人知道为什么吗?
可能是 this.literalsService.getValue('Ngwa.Ngwa', literal);
returns 字符串。
如果是这种情况,您需要将其转换为 Observable:
of(this.literalsService.getValue('Ngwa.Ngwa', literal));
同时 forkJoin
等待所有给定的流完成,然后发出值。
正如用户@ShamPooSham 和@martin 在评论中所说,问题是方法 this.literalsService.getValue ('Ngwa.Ngwa', literals)
正在观察文字的变化,而方法 forkJoin
从未启动,因为文字不是完全的。
我创建了一个单独的方法来解决问题,并在初始方法中添加了一个 take
:
新方法:
translateString(text: string): Observable<string> {
return this.literalsService.getValue('Ngwa.Ngwa', text).pipe(
switchMap((translated) => {
if (!translated.startsWith('N/A')) {
return of(translated);
} else {
return this.literalsService.getValue('Ngwa.Ngwa', text).pipe(
skip(1), // First event translation comes not translated
map((secondTryTranslation) => {
return secondTryTranslation;
}));
}
}),
);
}
第一种方法更正:
translateLiterals() {
const literalsToTranslate: string[] = [
'certificate_title',
'with_address',
'hereby',
'declare',
'electronic_records',
'proceeded_to_shipment',
'return_address',
'return_address',
'addressee',
'message_subject',
];
const newArray: Observable<string>[] = [];
literalsToTranslate.map(literal => newArray.push(this.translateString(literal).pipe(take(1)))); //<--- Here is the correction
this.literalsArray$ = forkJoin([...newArray]).pipe(map((results) => {
return results;
}));
}```