SWITCHMAP operator throws error when used with ParamMap

124 Views Asked by At

I'm trying to get data by special types.

So I use 2 observables.

  this.activatedRoute.paramMap
    .pipe(
      takeWhile(() => this.alive),
      switchMap((params: ParamMap) => this.setProductStatus(params.get('status'))
      )
    );

setProductStatus(status: any): void {
  this.isLoading= true;

  switch (status) {
  case 'listing':
    //some logic here;
    break;
  case 'purchased':
    //logic here;
    break;
  case 'in-review':
    //logic here
    break;
  default:
    this.route.navigate(['/path/relativePage']);
    break;

  }

  this.productService.getProductByStatus(status)

    .subscribe(data => {
        this.isLoading= false;
        this.products = data
      },
      (error) => {
        console.log(error)
      });

As you can see, I've tried to use a switchMap operator but it throws an error:

Argument of type '(params: ParamMap) => void' is not assignable to parameter of type '(value: ParamMap, index: number) => ObservableInput'. Type 'void' is not assignable to type 'ObservableInput'.

I've found a similar example on

https://www.pluralsight.com/guides/switching-to-most-recent-observable-switchmap

1

There are 1 best solutions below

3
Kavinda Senarathne On

You need to import the switchMapoperator to use it. Add the following line at the top of your file, where you have your other imports:

import 'rxjs/add/operator/switchMap';