【发布时间】:2019-06-29 06:23:58
【问题描述】:
类似这个问题的代码Performance of using same observable in multiple places in template with async pipe
但在 rxjs6 中不起作用?
https://stackblitz.com/edit/angular-shared-fail
import { Component, Input } from '@angular/core';
import {Observable, of, range, zip} from 'rxjs';
import {filter, map, share, switchMap, tap, toArray} from 'rxjs/operators';
@Component({
selector: "some-comp",
template: `
Sub1: {{squareData$ | async}}<br>
Sub2: {{squareData$ | async}}<br>
Sub3: {{squareData$ | async}}
`
})
export class HelloComponent {
squareData$: Observable<string> = range(0, 10).pipe(
map(x => x * x),
tap(x => console.log(`CalculationResult: ${x}`)),
toArray(),
map(squares => squares.join(', ')),
share() // remove this line and the console will log every result 3 times instead of 1
);
}
每个数字记录 3 次。预计一次。
【问题讨论】: