Professional Documents
Culture Documents
Rxjs Going Reactive Slides
Rxjs Going Reactive Slides
Rxjs Going Reactive Slides
EMPTY
throwError
"products$ | async"
products: Product[] = [];
ngOnInit() {
this.productService.getProducts()
.subscribe(products => this.products = products);
}
products$: Observable<Product[]>;
ngOnInit() {
this.products$ = this.productService.getProducts();
}
<div *ngIf="products">
<table>
<tr *ngFor="let product of products">
<td>{{ product.productName }}</td>
<td>{{ product.productCode }}</td>
</tr>
</table>
<table>
<tr *ngFor="let product of products">
<td>{{ product.productName }}</td>
<td>{{ product.productCode }}</td>
</tr>
</table>
catchError
catchError(this.handleError)
-
-
return this.http.get<Product[]>(this.productsUrl)
.pipe(
catchError(err => {
console.error(err);
return of([{ id: 1, productName: 'cart'},
{ id: 2, productName: 'hammer'}]);
});
ngOnInit() {
this.productService.getProducts()
.subscribe(
products => this.products = products,
err => this.errorMessage = err
);
}
catchError
return this.http.get<Product[]>(this.url)
.pipe(
catchError(err => {
console.error(err);
return of(
[{ id: 1, productName: 'cart'},
{ id: 2, productName: 'hammer'}
]);
catchError(err =>...)
})
);
catchError
catchError
-
-
-
-
-
-
return this.http.get<Product[]>(this.productsUrl)
.pipe(
catchError(err => {
console.error(err);
return throwError(err);
});
throwError
throwError(err)
-
throwError
-
private productsUrl = 'api/products';
getProducts(): Observable<Product[]> {
return this.http.get<Product[]>(this.productsUrl)
.pipe(
catchError(this.handleError)
);
}
private handleError(err) {
// ...
return throwError(errorMessage);
}
this.productService.getProducts()
.subscribe(
products => this.products = products,
err => this.errorMessage = err
);
this.products$ = this.productService.getProducts()
this.productService.getProducts();
.pipe(
catchError(err => {
this.errorMessage = err;
return ???;
})
);
EMPTY
return EMPTY;
-
this.products$ = this.productService.getProducts()
.pipe(
catchError(err => {
this.errorMessage = err;
return EMPTY;
})
);
checkAlways
@Component({
templateUrl: './product-list.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
private productsUrl = 'api/products';
getProducts(): Observable<Product[]> {
return this.http.get<Product[]>(this.productsUrl)
.pipe(
catchError(this.handleError)
);
}
ngOnInit() {
this.products$ = this.productService.getProducts()
.pipe(
catchError(err => {
this.errorMessage = err;
return EMPTY;
})
);
}
private productsUrl = 'api/products';
products$ = this.http.get<Product[]>(this.productsUrl);
products$ = this.productService.products$;
private productsUrl = 'api/products';
products$ = this.http.get<Product[]>(this.productsUrl)
.pipe(
catchError(this.handleError)
);
products$ = this.productService.products$
.pipe(
catchError(err => {
this.errorMessage = err;
return EMPTY;
})
);
-
-
private productsUrl = 'api/products';
products$ = this.http.get<Product[]>(this.productsUrl)
.pipe(
catchError(this.handleError)
);
products$ = this.productService.products$
.pipe(
catchError(err => {
this.errorMessage = err;
return EMPTY;
})
);
@Component({
templateUrl: './product-list.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
<div *ngIf="products$ | async as products">
<table>
<tr *ngFor="let product of products">
<td>{{ product.productName }}</td>
<td>{{ product.productCode }}</td>
</tr>
</table>
-
-
-
catchError(err => {
this.errorMessage = err;
return EMPTY;
})
catchError(err => {
console.error(err);
return throwError(err);
})
catchError
throwError
products$ = this.http.get<Product[]>(this.productsUrl)
.pipe(
catchError(err => {
console.error(err);
return throwError(err);
});