【问题标题】:RxJs ReplaySubject does not replay on Ionic2RxJs ReplaySubject 不在 Ionic2 上重播
【发布时间】:2016-10-04 15:23:56
【问题描述】:

我在 Ionic2/RC0 上,我正在为我的应用程序实现用户服务。

@Injectable()
export class UserService {
    UserWhatever: ReplaySubject<User> = new ReplaySubject<User>();



    constructor(private _util: UtilService,
        private _app: AppService,
        private _http: Http) {
        console.log("I am UserService. Just got created")

        this.UserWhatever.subscribe(
            (data) => {
                console.log("User service got a USER");
            }
        )


    }

    login(element) {
        return this.getToken(element).flatMap(
            (data) => {
                return Observable.forkJoin([Observable.of(data),
                this.readUser(data)
                ]);
            }
        ).map(data => {
            localStorage["jwt"] = data[0];
            this._util.sendToast("logged");
            console.log("Http call received a USER")
            let guser: User = data[1];
            console.log("Called next ")
            this.UserWhatever.next(guser)
        });
    }

当我从 HomeComponent 中可观察的 login() 获得完整响应后,我将用户发送到 DashboardComponent

export class LoginComponent implements OnInit {

    subs() {
        this._user.UserWhatever.subscribe(
            (data) => {
                console.log("Login Component got a Replayed USER");
            })
    }


    login() {
        this.isDisabled = true;
        this._user.login(this.loginForm)
            .subscribe(
            data => {
                this.subs()   
                this.nav.push(DashboardPageComponent);
            }    
    }

我从我的 userService 订阅 ReplaySubject。这里是问题出现的地方。它不会在订阅时重播主题。如果我碰巧在 ReplaySubject 上再次调用 next(),一切正常。

export class DashboardPageComponent implements OnInit, OnDestroy, AfterViewInit {
    public user: User;


    constructor(private _app: AppService,
        private _device: DeviceService,
        private _user: UserService,
        private _store: Store<any>,
        private _nav: NavController) {
        console.log("Dashboard component")
    }

    ngOnInit() {

        this._user.UserWhatever.subscribe(
            (data) => {
                this.user = data;
                console.log("Dashboard finally received a User")
                console.log(data)
            }
        )
    }

这一切都像常规的 Rxjs.Subject() 一样工作,但我需要重播功能。我在 rxjs.beta-12

更新日志

I am UserService. Just got created
Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
Native: tried calling StatusBar.styleDefault, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator
Http call received a USER
Called next 
User service got a USER
Login Component got a Replayed USER
I am UserService. Just got created
Dashboard component

【问题讨论】:

  • 我喜欢问题标题....doesn't replay...;) he he!!!
  • 您确定将 same UserService 实例注入到DashboardPageComponentLoginComponent 中吗? DashboardPageComponent 是否有可能获得一个 new 实例(因此是新的空 ReplaySubject)?
  • @Brandon 我怎么确定?如果这就是你的意思,我正在从同一个文件中导入。我很确定这就是正在发生的事情,但不知道为什么。如果我后来碰巧打电话给下一个,订阅就会起作用
  • 在UserService构造函数中放一个console.log语句,看看它打印了多少次。
  • @Brandon 在创建时看起来像this.nav.push(DashboardPageComponent);create another another UserService。这应该发生吗?

标签: angular ionic2 rxjs5


【解决方案1】:

根据您的代码(基本上是正确的)和测试,您的问题是 UserService 正在为每个需要它的组件重新创建。您需要将其设为“单例服务”,这意味着单个实例将与所有需要它的组件共享。

我不熟悉离子。这是one thread,你可以试试。如果没有帮助,我建议您打开第二个 SO 问题,询问如何在 Ionic 中使您的 UserService 成为单例。

显然更改DashboardPageComponent 的构造函数以将_user 属性声明为公共而不是私有将允许Ionic 注入相同的实例而不是创建一个新实例。 https://forum.ionicframework.com/t/rc0-typescript-private-vs-public-keyword/64863

【讨论】:

  • 现在知道了! constructor(private _user:UserService) 应该是 constructor(public _user:UserService)。我愿意编辑你的答案以添加这个我会接受的正确答案
  • 我已将问题重命名为 Ionic2 特定
  • @CESCO 好的,我添加了一个段落,希望能捕捉到您的发现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-01
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
相关资源
最近更新 更多