RxJs ReplaySubject 不会在 Ionic2 上重播

RxJs ReplaySubject does not replay on Ionic2

我在 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 的地方。这就是问题 appear.It 在订阅时没有重播该主题的地方。如果我碰巧在 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

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

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

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