Search code examples
angularionic-frameworkionic4

How to get canGoBack functionality in Ionic 5?


I'm trying to put a back button in Ionic app and to hide the back button when it's in the root, which the root is not fixed and can be changed based on the flow. I found the following code here:

...
constructor(
    private router: Router,
    private ionRouterOutlet: IonRouterOutlet
) {

}

ngOnInit() {
    this.canGoBack    = this.ionRouterOutlet.canGoBack();
...

It would be nice if it works but IonRouterOutlet is component and cannot be injected to another component like this. Any idea about how can it be done in Ionic 5 with angular?


Solution

  • @ViewChild(IonRouterOutlet, { static : true }) routerOutlet: IonRouterOutlet;
    
        // if (this.routerOutlet.canGoBack()) or whatevet...