【发布时间】:2021-12-30 12:48:30
【问题描述】:
我有一个简单的 Angular 应用程序女巫从 Spring-Boot 后端获取值
export class UserDto {
constructor(
public login: string,
public password: string,
) {
}
}
export class AuthService {
private url = '....';
getUser() {
return this.http.get<UserDto[]>(this.url);
}
}
在我的组件中,我有创建值映射的函数:
constructor(private auth: AuthService){}
private getMapOfUsers() {
const priceListMap: Map<string, string> = new Map<string, string>();
this.auth.getUser().subscribe(res => {
res.map(item => {
priceListMap.set(item.login, item.password);
}
);
});
return priceListMap;
}
当我使用时:
getLoginData() {
console.log(this.getMapOfUsers());
}
我有控制台结果:
来自 UserDto 的登录以红色划线
我有Map(0),但大小为 18...当我尝试获取实际大小时,此地图的大小 = 0。我可以在“条目”中看到对象,但我无法以任何方式获取它们。
getLoginData() {
console.log(this.getMapOfUsers().size); // result = 0
}
如何获得可以映射这些对象的解决方案,然后如何使用给定密钥作为登录名找到密码????
【问题讨论】:
-
您可能需要阅读 Observables 的异步特性。您在 ons 发出之前返回 priceListMap。
标签: angular typescript