简体   繁体   中英

Angular 6 subscribe changes 'this' context

I got my service working back on angular 5, now updating to angular 6 seems to need a bit of tweaking, every tutorial or video I find is labeled as angular 6 but is using angular 5 or a previous version, anyways, here is my code:

Service

@Injectable()
export class UserService {

  constructor(private http: HttpClient) { }

  getUsers() {
    return this.http
      .get(environment.server.url + 'users');
  };

  addUser(user: User) {
    return this.http
      .post(environment.server.url + 'users/', user);
  };
}

Component

ngOnInit() {
   this.userService.getUsers().subscribe(res => {
     this.users = res;
   });
 };

When debugging I get the desired response from the server, an array of users but what fails is the this.users = res , this is bound to SafeSubscriber , not to the correct one with the users object.

Using fat-arrow I get the safesubscriber bound to 'this' 在此处输入图片说明

使用Chrome控制台时,似乎是Visual Studio代码调试器的错误,但可以正确显示。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM