Close Modal in Ionic 4 by Back Button

后端 未结 6 1999
名媛妹妹
名媛妹妹 2021-02-03 12:38

I have a Modal in Ionic 4. I\'d like to close it, when a user press the back button on her mobile (or the bac

6条回答
  •  [愿得一人]
    2021-02-03 13:10

    Enol's answer helped me find a solution, thanks for that.

    platform.registerBackButtonAction does no longer exist in v4. I tried platform.backButton.subscribe instead, but it didn't work. What works is this:

    private backbuttonSubscription: Subscription;
    
    constructor(private modalCtrl: ModalController) {
    
    ngOnInit() {
        const event = fromEvent(document, 'backbutton');
        this.backbuttonSubscription = event.subscribe(async () => {
            const modal = await this.modalCtrl.getTop();
            if (modal) {
                modal.dismiss();
            }
        });
    }
    
    ngOnDestroy() {
        this.backbuttonSubscription.unsubscribe();
    }
    

提交回复
热议问题