Search code examples
angularobservableobservers

How can I use multiple statements in an Angular ES6 Observer function?


The original convention is to do something like this

    this.productService.getProducts().subscribe({
      next: products => {
        this.products = products
        this.filteredProducts = products
      },
      error: err => this.errorMessage = err

However when trying to use the ES6 shortcut below, I can't get the next function to handle multiple statements. If I use one statement, it works fine.

    //   next(products) {
    //     this.products = products
    //     this.filteredProducts = products},
    //   error(err) {this.errorMessage = err}

Is it possible to use write multiple lines here?


Solution

  • The way you are using it should work. Here's a stack that I created to verify.. https://stackblitz.com/edit/typescript-ug28l2