How to show a confirmation message before exiting the Ionic 3 tab

My ionic 3 app has ion tabs with two tabs. When switching from tab to tab, I need to show a confirmation message (using AlertController) so that the user cannot change the current tab if he does not confirm his choice. Is this possible in the ionic? I tried to show a confirmation message when changing the tab. However, I could not prevent the appearance of a new tab.

Thanks.

+5
source share
1 answer

You can implement such things using the Nav guard. You can find them in the ion docs for NavController .

An implementation might look something like this:

ionViewCanEnter(): Promise<any> { return new Promise((resolve, reject) => { let alert = this.alertCtrl.create({ title: 'Alert', message: 'Please confirm ...', buttons: [ { text: 'Cancel', role: 'cancel', handler: () => { reject(); }, }, { text: 'Confirm', handler: () => { resolve(); }, }, ], }); alert.present(); }); } 

Use ionViewCanEnter() since ionViewCanLeave() does not currently work (at least when working with tabs).

+3
source

Source: https://habr.com/ru/post/1271788/


All Articles