angular - 将数据保存在 observable (ionic 4)
问题描述
我想在登录后保存用户数据,然后在个人资料页面中检索数据。我有一个简单的user.service.ts
@Injectable()
export class UserService {
private _user: BehaviorSubject<IRegisterUser> = new BehaviorSubject<IRegisterUser>(null);
public user$: Observable<IRegisterUser> = this._user.asObservable();
constructor(public afAuth: AngularFireAuth) {}
public setUser(user) {
this._user.next(user)
}
成功更新了行为主体。
后来我尝试访问profile.page.ts中的 observable
export class ProfilePage implements OnInit {
constructor(public userSrv: UserService) { }
ngOnInit() {
this.userSrv.user$.subscribe(resp=> {
console.log(resp)
})
}
}
但我的回答是空的
我已将 user.service.ts 放在 app.module 和 profile.module 的提供者中,但我可能会错过其他内容。
解决方案
我已将 user.service.ts 放在 app.module 和 profile.module 的提供者中,但我可能会错过其他内容。
有问题,UserService
从profile.module
提供者中删除。现在profile.module
有他自己的提供者UserService
而不是使用全局提供者。
推荐阅读
- angular - 如何在 Angular 中显示来自 REST API(嵌入式)的 MongoDB 数据?
- python - 使用 RegEx 从 .h 文件中提取结构定义以根据 [XXX] 标准检查代码一致性
- visual-studio-code - 为什么没有程序在 Visual Studio 代码中运行?
- c - Bison yyerror 忽略语法上的下一个标记
- c++ - 如何跨线程恢复增强纤维?
- javascript - 如何使用 AngularJS 使文档准备好有条件?
- swift - Swift 中的 Mapbox 3D 地形路线
- python - 执行有界方法时未更新 Python 字典键
- php - 多单选按钮数组通过codeigniter插入数据库
- css - 如何在 React Google Charts 中的直方图上放置一条线