【问题标题】:Ionic2: Unsubscribe Event to Avoid Duplicate Entries?Ionic2:取消订阅事件以避免重复条目?
【发布时间】:2017-05-09 02:33:13
【问题描述】:

我关注了this tutorial,它概述了在 Ionic 2 应用程序中添加监控信标。我让它工作得很好:当视图加载时,它会初始化并开始监听信标:

home.ts

    ionViewDidLoad() {
        this.platform.ready().then(() => {
            this.beaconProvider.initialise().then((isInitialised) => {
                if (isInitialised) {
                    this.listenToBeaconEvents();
                }
            });
        });
    } 

这会调用listenToBeaconEvents 函数,该函数会在视图中使用所有信标填充列表:

home.ts

    listenToBeaconEvents() {
        this.events.subscribe(‘didRangeBeaconsInRegion’, (data) => {

            // update the UI with the beacon list
            this.zone.run(() => {

                this.beacons = [];

                let beaconList = data.beacons;
                beaconList.forEach((beacon) => {
                    let beaconObject = new BeaconModel(beacon);
                    this.beacons.push(beaconObject);
                });

            });

        });
    }

我可以使用this.beaconProvider.stopRanging() 来停止测距,该this.beaconProvider.stopRanging() 从以下函数调用函数:

beacon-provider.ts

    stopRanging() {
        if (this.platform.is('cordova')) {
            // stop ranging
            this.ibeacon.stopRangingBeaconsInRegion(this.region)
                .then(
                () => {
                    console.log('Stopped Ranging');
                },
                error => {
                    console.error('Failed to stop monitoring: ', error);
                }
                );
        }
    }

我遇到的问题是 - 在原始教程中,信标列表显示在根部,没有其他导航。我已将其移至不同的视图,如果用户退出并重新进入视图,它会重新初始化并加载所有内容,从而导致重复的列表条目。

我尝试在 beacon-provider.ts 中创建一个函数以在视图退出之前调用,但我不知道如何防止订阅/事件重复。

我尝试过this.delegate.didRangeBeaconsInRegion().unsubscribe() 和其他一些变体,但它们都会导致运行时错误。

【问题讨论】:

    标签: ionic-framework ionic2 ibeacon


    【解决方案1】:

    在您的情况下,您使用的是 Ionic 的 Events API,它有自己的 unsubscribe(topic, handler) 函数。

    在您的组件中,当您需要取消订阅时,您应该使用相同的主题调用它:

    this.events.unsubscribe(‘didRangeBeaconsInRegion’);
    

    这将删除您可能为didRangeBeaconsInRegion 注册的所有处理程序

    如果你想取消订阅一个特定的功能,你必须注册一个命名的处理程序,你可以通过取消订阅发送它。

    this.events.unsubscribe(‘didRangeBeaconsInRegion’,this.mySubscribedHandler);
    

    你的 home.ts 看起来像:

     mySubscribedHandler:any = (data) => {
    
                // update the UI with the beacon list
                this.zone.run(() => {
    
                    this.beacons = [];
    
                    let beaconList = data.beacons;
                    beaconList.forEach((beacon) => {
                        let beaconObject = new BeaconModel(beacon);
                        this.beacons.push(beaconObject);
                    });
    
                });
    
            }
    
        listenToBeaconEvents() {
            this.events.subscribe(‘didRangeBeaconsInRegion’,this.mySubscribedHandler);
        }
    

    【讨论】:

    • 您能指定我在哪里声明订阅类属性吗?我在listenToBeaconEvents()、构造函数下的export class TakeTour 和beacon-provider.ts 中都尝试过,但我在这三个方面都遇到了错误。谢谢!
    • 您需要在您订阅的类中将其声明为类变量.. 还记得import { Subscription } from 'rxjs/Subscription';
    • 好的大部分错误都消失了,现在我只剩下一个:“类型'void'不可分配给类型'Subscription',然后this.beaconEventSubscription被突出显示为错误行。我在顶部导入Subscription,在构造函数中导入public beaconEventSubscription: Subscription
    • 它不应该在构造函数参数中..它只是一个类变量..在构造函数外部的类中声明它
    • 嗨@suraj,感谢您继续提供帮助,我仍然收到“类型'无效'不可分配给类型'订阅'。我已经更新了上面的问题以显示完整代码以防我遗漏了什么
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 2016-04-27
    • 2012-01-26
    相关资源
    最近更新 更多