[英]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: 我让服务恢复到角度5,现在更新到角度6似乎需要一些调整,我发现的每个教程或视频都标记为角度6,但使用角度5或以前的版本,无论如何,这是我的代码:
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. 调试时,我从服务器获得了所需的响应,该响应由用户组成,但失败的是this.users = res ,这绑定到SafeSubscriber ,而不是与users对象正确的绑定。
Using fat-arrow I get the safesubscriber bound to 'this' 使用胖箭头,我将安全订户绑定到“ this”
使用Chrome控制台时,似乎是Visual Studio代码调试器的错误,但可以正确显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.