将数据保存在 observable (ionic 4) 中

Save data in 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

中的可观察对象
export class ProfilePage implements OnInit {

  constructor(public userSrv: UserService) { }

  ngOnInit() {
   this.userSrv.user$.subscribe(resp=> {
     console.log(resp)
   })

  }

}

但我的回复是null

我已将 user.service.ts 放入 app.module 和 profile.module 的提供程序中,但我可能遗漏了其他内容。

I have put the user.service.ts in the providers of app.module and profile.module but I probably miss something else.

出现问题,从 profile.module 个提供商中删除 UserService。现在 profile.module 拥有自己的 UserService 供应商,而不是使用全球供应商。