Assuming you're building a mobile app here:
This is problematic for me because I want to show a toast when
internet connectivity issue is detected,
I think you're trying to solve the wrong problem. You should not need to worry about the lazy loading aspect, instead you should make sure all the files are available inside the app bundle!
I don't understand how the toast can even have lazy-loaded code to
begin with.
That is easy to explain. The code that actually renders the component in the screen, that decides the position, size, colour, font, duration etc.. that doesn't need to be in the core modules so it's only loaded when you try to use it for the first time. All that has to be loaded is the instruction confirming that component exists and how to fetch it.
This is problematic for me because I want to show a toast when
internet connectivity issue is detected, so the code for the toast
needs to be entirely preloaded.
This is not entirely true. It needs to be available, not pre-loaded. I think this question can be resolved if you can work out why your files are being loaded from a remote server (like in the example you posted in your question).
To Illustrate, I've just created a brand new ionic app and executed it:
ionic start myapp tabs
ionic serve
I've changed my tab1.page.ts
to include the ion-toast code and a click event on one of the links generated by the example:
Tab1.page.ts:
import { Component } from '@angular/core';
import { ToastController } from '@ionic/angular';
@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
constructor(public toastController: ToastController) {}
doIt() {
console.log('done it');
this.presentToast();
}
async presentToast() {
const toast = await this.toastController.create({
message: 'Your settings have been saved.',
duration: 2000
});
toast.present();
}
}
Tab1.page.html:
<ion-item (click)="doIt()">
<ion-icon slot="start" color="medium" name="book"></ion-icon>
<ion-label>Ionic Documentation</ion-label>
</ion-item>
Look at the screenshot below. My internet is off, and i can still lazy load the toast scripts because they're all in the available bundle I'm running. In a mobile app they should be bundled within your application instead of loaded remotely (opposed to what happens in their website).
Now IF you're running this on a webpage instead of in an app, you'll need to use one of the solutions presented in the other answers. I couldn't find out a decent way of injecting the toast scripts, so if I had to do it I would present an invisible toast while my app was loading, below the screen so it would be invisible.