Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 4 setRoot with Angular Router

Tags:

I'm upgrading my Ionic 3 project to the latest Ionic 4 and I have some trouble with Routing. In Ionic 3 I used setRoot just like this:

handler: () => navCtrl.parent.parent.setRoot(HomePage, 'logout', {animate: true}) 

The latest navCtrl of Ionic 4 has only goBack, goForward and goRoot, and I don't understand how to use parent. I found ActivatedRoute in Angular, but I don't think this is the right way. How can I do?

like image 663
Riccardo Avatar asked Aug 04 '18 09:08

Riccardo


People also ask

How do I redirect to another page in ionic?

to​ A redirect route, redirects "from" a URL "to" another URL. This property is that "to" URL. When the defined ion-route-redirect rule matches, the router will redirect to the path specified in this property.

How do I pass data from one page to another page in ionic 5?

To Pass data from Home page to About page we will need to import NavParams class. Since, I am using Ionic CLI to generate pages, class NavParams will already be imported in the about page.

How do I set default page in ionic 4?

to set a root page, you would be required to set your app routes in the router module. when you create a project with the ionic cli, the routing module is added for you automatically. check if the <base href="/" > has been added to the index. html file, if it's not there please add it.


2 Answers

Generally speaking, and citing this awesome article on this matter by Josh Morony:

In Ionic 4 with Angular routing, there is no root page to be defined.

Because Ionic 4 relies on Angular's router, the NavController has been changed to reflect this new reality, and for an Angular application there is no such a thing like "root" route. You simply transition between routes and the framework does the rest of the work.

Generally speaking, the methods navigateRoot, navigateBackward and navigateForward are here only to instruct Ionic on how to deal with animations. So you can use navigateRoot in Ionic 4 to accomplish the same of what you used setRoot on Ionic 3.

I strongly recommend that you read the aforementioned article, it covers a lot of what you need to know to migrate your routes from version 3 to version 4 of Ionic.

like image 133
Elvis Fernandes Avatar answered Oct 20 '22 05:10

Elvis Fernandes


With @angular/router one way to achieve the behavior that you expect is by using replaceUrl and skipLocationChange of the NavigationExtras here on official docs The code would something like this:

this.router.navigate([pageLink], {replaceUrl: true}) 

But yes, the referred navigateRoot doesnt exist on @angular/router as it was on ionic 3

like image 45
Igor Avatar answered Oct 20 '22 04:10

Igor