Skip to content

Instantly share code, notes, and snippets.

@kingsleytagbo
Last active November 18, 2020 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingsleytagbo/c03cb9d5901484fdb0d9857e453e1d6b to your computer and use it in GitHub Desktop.
Save kingsleytagbo/c03cb9d5901484fdb0d9857e453e1d6b to your computer and use it in GitHub Desktop.
Reusable Ionic Angular Toast Component
public async login(){
/* Inject the "Utility Provider" and reuse your toast component */
await this.utility.showToast(
'Error',
'Login not implemented',
{position:'top', duration:10000, showCloseButton:true, closeButtonText:'OK', color:'danger'}).then(toast =>{
toast.present();
});
}
import { Injectable } from '@angular/core';
import { ToastController } from '@ionic/angular';
@Injectable({providedIn: 'root'})
export class UtilityProvider{
constructor( private toast: ToastController){}
/**
* A custom reusable toast message
* Author: Kingsley Tagbo / https://github.com/kingsleytagbo
* @param header: the header / title of your toast message
* @param message: the body or main content of your toast message
* @param overrideOptions: an object with options for customizing your toast, e.g. {position: 'bottom'}
*/
async showToast(header?: string, message?:string, overrideOptions?:any): Promise<HTMLIonToastElement>{
const toastOptions = {header, message, position:'top', duration: 6000};
const options = Object.assign(toastOptions, overrideOptions);
const result = await this.toast.create(options);
return result;
}
}
@kingsleytagbo
Copy link
Author

Reusable + Custom Ionic Angular Toast Message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment