gpt4 book ai didi

javascript - ionic 5 : Issues in changing Events to Observable

转载 作者:行者123 更新时间:2023-12-01 00:07:47 24 4
gpt4 key购买 nike

自从 Ionic 5 发布以来,我决定尝试一下。我了解到他们现在使用 Oberservables 而不是 Events。我的代码中有以下事件:

在我的构造函数中像这样订阅:

                this.events.subscribe('go-to-slide', (tab, id) => {
this.currentID = id;
if (id === 0) {
this.showLeftButton = false;
}
// if data has already been loaded
if (this.dataLoadedFlag === true) {
// from tile
if (this.globalVariableService.comesFromTileClick === true) {
if (this.globalVariableService.languageChanged === true) {
this.languageChanged = false;
this.slidesComponent.slides.slideTo(id, 0);
this.getSectorTitlesAndColours();
this.dataLoadedFlag = true;
this.updateHeader(id);
} else if (this.globalVariableService.languageChanged === false) {
this.updateHeader(id);
}
} else if (this.globalVariableService.comesFromTileClick === false) {

}
} else if (this.dataLoadedFlag === false) {

if (this.globalVariableService.comesFromTileClick === true) {
this.slidesComponent.slides.slideTo(id, 0);
this.getSectorTitlesAndColours();
this.dataLoadedFlag = true;
this.updateHeader(id);
} else if (this.globalVariableService.comesFromTileClick === false) {
this.getSectorTitlesAndColours();
this.showRightButton = true;
this.dataLoadedFlag = true;
this.updateHeader(0);
}
}
const tempGlobalVariableService = this.globalVariableService;
// tslint:disable-next-line: only-arrow-functions
setTimeout(function() {
tempGlobalVariableService.comesFromTileClick = false;
}, 500);
});
}

并在不同的组件/方法中像这样发布事件:

this.events.publish('go-to-slide', 1, 1);

我尝试了不同的方法来将此代码更改为可观察的,但我似乎找不到正确的方法。

有人已经尝试过这个并且可以帮助我吗?谢谢

最佳答案

我已经在这里回答了https://stackoverflow.com/a/60246906/2405040

您可以为此创建一个小型服务:

import {Injectable} from '@angular/core';
import {Subject} from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class GlobalFooService {

private fooSubject = new Subject<any>();

publishSomeData(data: any) {
this.fooSubject.next(data);
}

getObservable(): Subject<any> {
return this.fooSubject;
}
}

完整示例请引用其他答案。

顺便说一句,讨论这个评论:

I have learned that they are now using Oberservables instead of Events

事实并非如此。基本上,他们从 Ionic 5 中删除了 Events 服务,并要求我们通过创建我们自己的类似实现来使用 Observables 来代替。

自 Ionic 3 起,他们可能一直在内部使用 Events。但自 Ionic 4 起,他们已经删除了内部使用,因为 Obersables 已经发展得如此之快,而且它是 Angular 和 Ionic 的支柱.

关于javascript - ionic 5 : Issues in changing Events to Observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60257042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com