Last active
November 18, 2020 02:21
-
-
Save kingsleytagbo/c03cb9d5901484fdb0d9857e453e1d6b to your computer and use it in GitHub Desktop.
Reusable Ionic Angular Toast Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Author
kingsleytagbo
commented
Sep 29, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment