angular - 如何在Angular中将数据传递给订阅者
问题描述
我有一个订阅“用户”的以下服务。
public getEmployees(): Observable<User> {
const url = `${this.baseUrl}/employee`;
return this.http.get<User>(url);
}
我的问题是如何使用用户数据调用 getEmployees。我正在尝试使用以下代码,但出现错误?
const _user: User = {
userId: "30294
}
this.userAccountService.getEmployees().subscribe((userprofile: _user) => {
console.log(userprofile.firstName);
});
错误:_user 引用了值,但在这里使用了一个类型
解决方案
您正在_user
用作类型userprofile: _user
像这样修复:
const _user: User = {
userId: "30294
}
this.userAccountService.getEmployees().subscribe((userprofile: User) => {
console.log(userprofile.firstName);
});
推荐阅读
- html - 如何使用 flexbox 定义动态表格布局
- visual-studio-code - 解决合并冲突的 VS 代码通常将当前和传入作为 2 个完全独立的块呈现,不费力寻找差异
- r - 计算数据框中变量中多个字母的出现次数?
- jms - Mule 4 - 如何为 JMS 连接器设置会话 ID
- php - 动态创建的 php bind_param 未按预期工作
- r - 在 r 中重新排序列时选择的数据框未定义列中的错误
- java - 无法将 GCP 签名 URL 与 Azure 认知语音转文本一起使用
- javascript - Apollo 缓存数据更改但未反映在组件中
- flutter - Flutter 图像选择器验证器
- kotlin - 在 Kotlin 中通过 fieldName 访问对象值