I have a toast notification component called ToastComponent
which I want to call from any other component. I implemented it like this:
ToastComponent
:
export class ToastComponent implements OnInit {
constructor() {}
showToast() {
// some code
}
}
app.component.html
:
<llqa-main-container>
<llqa-header></llqa-header>
<div class="content-container">
<main class="content-area">
<llqa-toast></llqa-toast> <!-- ToastComponent which I want to call -->
<router-outlet></router-outlet>
</main>
</div>
</llqa-main-container>
UserManagementComponent
which is inside the <router-outlet>
:
export class UserManagementComponent implements OnInit {
@ViewChild(ToastComponent) toast: ToastComponent;
constructor() {}
someSaveMethod() {
this.toast.showToast() // throws error below
}
}
On calling the someSaveMethod()
method, I'll get the error that toast
is undefined.
If I take <llqa-toast></llqa-toast>
out of the app.component.html
and put it on top of the user-management.component.html
, it works fine, but then I have to put it in every component. How can I get this to work?