组件.ts
// Component containing function
Public myData(){
this.myService.getData()
.pipe(
take(1),
catchError((err: any) => {
this.myRows = [];
return throwError(err);
}),
finalize(() => {
console.log('In Finally');
})
)
.subscribe((rows) => {
this.myRows = rows;
// Do something
});
}
Run Code Online (Sandbox Code Playgroud)
myService.ts
// Service Call
public getData(): Observable < customer[] > {
const url = 'some url';
return this.http.get(url).pipe(
map(this.checkForError),
catchError((err: HttpErrorResponse) => {
return throwError(err);
}),
map(this.myJson),
share()
);
}
Run Code Online (Sandbox Code Playgroud)
规格
// Test Case
it('should test', () => {
let test = …Run Code Online (Sandbox Code Playgroud) import {
Observable,
BehaviorSubject
} from 'rxjs';
import {
finalize,
share
} from 'rxjs/operators'
export class someComponent() {
public count$ = new BehaviorSubject < any > (0);
public constructor() {
this.shareResponse()
.pipe(
finalize(() => {
console.log('finalize called');
}))
.subscribe((event: any) => {
// Do something
});
}
public shareResponse(): Observable < any > {
return this.count$.pipe(share());
}
public countChanged(event) {
this.count$.next(event);
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<some-tag(countChanged) = (countChanged($event)) > < /some-tag>
Run Code Online (Sandbox Code Playgroud)