Angular TypeError:在执行传递的函数时无法设置属性
我不熟悉函数式编程。在将函数传递给类型脚本中的另一个函数时,我遇到了一个错误。这是我的密码:-Angular TypeError:在执行传递的函数时无法设置属性,angular,typescript,Angular,Typescript,我不熟悉函数式编程。在将函数传递给类型脚本中的另一个函数时,我遇到了一个错误。这是我的密码:- export class NavigationComponent implements OnInit { constructor(public globals: Globals, public auth: AuthService, public placeService: PlaceService) { auth.handleAuthentication(); if (auth.
export class NavigationComponent implements OnInit {
constructor(public globals: Globals, public auth: AuthService, public placeService: PlaceService) {
auth.handleAuthentication();
if (auth.isAuthenticated) {
console.log(' userName ' + this.firstName + ' userName ' + this.userName + ' dp ' + this.dpUrl);
this.fetchProfile();
console.log(' userName ' + this.firstName + ' userName ' + this.userName + ' dp ' + this.dpUrl);
}
}
ngOnInit() {
this.getCountries();
}
private firstName: any;
private userName: any;
private dpUrl: any;
private fetchProfile() {
this.auth.getProfileReponse(this.processResponse);
}
public processResponse(userResponse: Observable<HttpResponse<UserResp>>) {
userResponse.subscribe(resp => {
const user: User = { ...resp.body }.flatUser;
console.log('user');
console.log(user); // printing user perfetcly
console.log(user.firstName); // printing user perfetcly
this.firstName = user.firstName; // facing error here
this.userName = user.userName;
this.dpUrl = user.dpURL;
// console.log(this.flatUser);
});
}
}
为什么这个未定义的“名字”,我使用的是angular 6测试版。请帮我解决这个问题。这实际上是未定义的
的上下文。请尝试以下方法:
this.auth.getProfileReponse(() => this.processResponse);
箭头语法将与此绑定,它应该可以防止错误它起作用了,爸爸,你真漂亮。非常感谢。可能是副本
this.auth.getProfileReponse(() => this.processResponse);