首页 > 解决方案 > 将数据保存在 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 的提供者中,但我可能会错过其他内容。

标签: angularionic-frameworkionic4injectable

解决方案


我已将 user.service.ts 放在 app.module 和 profile.module 的提供者中,但我可能会错过其他内容。

有问题,UserServiceprofile.module提供者中删除。现在profile.module有他自己的提供者UserService而不是使用全局提供者。


推荐阅读