0

I'm new to Angular, and I try to build a web following a tutorial. But it said the way I use subscribe is too old, could you help me to change it?

**
employees: Employee[] = []
  constructor(private employeeService: EmployeeService){}
  public getEmployees():void {
    this.employeeService.getEmployees().subscribe(
      (res:Employee[])=>{
        this.employees = res
      },
      (error:HttpErrorResponse)=>{
        alert(error.message)
      }
    )
  }
**
  • try using this.. .subscribe({ complete: () => { ... }, error: () => { ... }, next: () => { ... } }); – prograk Mar 09 '22 at 04:44
  • It's not that it's old, it's generally a better pattern to pass the observable to the template (for example) and use the async pipe, not managing subscriptions yourself – Phix Mar 09 '22 at 04:54

1 Answers1

0

I'm also new to Angular and ran into the same issue. I think the problem is just syntax. There need to be curly braces surrounding the subscribe parameters. Also, at the beginning of the response handler, add "next:" and at the beginning of the error handler, add "error:"

this.employeeService.getEmployees().subscribe({
  next: (res:Employee[])=>{
    this.employees = res
  },
  error: (error:HttpErrorResponse)=>{
    alert(error.message)
  }
})
Kelly H
  • 1
  • 2