Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use routing in IONIC 3

I am developing an Ionic 3 application. Right now I am using NavController for routing and switching pages.

Ex : this.navCtrl.push(DetailsPage);

But I need to use Angular routing now.

I found similar question for Ionic 2, but does this work in Ionic 3 as well?

Can someone elaborate this?

like image 305
Sangwin Gawande Avatar asked Aug 08 '17 09:08

Sangwin Gawande


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.

What is ion router?

The router is a component for handling routing inside vanilla and Stencil JavaScript projects. Note: this component should only be used with vanilla and Stencil JavaScript projects. For Angular projects, use ion-router-outlet and the Angular router. Apps should have a single ion-router component in the codebase.

How does router navigate work?

Angular router traverses the URL tree and matches the URL segments against the paths configured in the router configuration. If a URL segment matches the path of a route, the route's child routes are matched against the remaining URL segments until all URL segments are matched.


2 Answers

Check this link for the detail of NavController link. Which you have to import into your current ts file, followed by =>

There are two ways of navigation we can make use in ionic

1) DeepLink

2) Component navigation stack

DeepLink

deeplink navigation acts like webpage navigation like below example you have to use @ionicpage anotation in order to use deeplink navigation

https://locallhost:8000/#/HomePage/SecondPage

Component Navigation

You have to import your component in the respected ts file in order to navigate

There are three key words push, pop, setRoot.

  1. setRoot

    Example:

    this.navCtrl.setRoot(HomePage);

    (or)

    this.navCtrl.setRoot("HomePage"); //DeepLink navigation

    Used to make the component as Root page, in other words, it creates an empty navigation stack where homepage is the root.

  2. push

    Example:

    this.navCtrl.push(SecondPage);

    (or)

    this.navCtrl.push("SecondPage"); //DeepLink navigation

    The above example has push keyword where the navigation stack has one component inside its stack followed by Homepage. I mean, after homepage component, you will be having secondpage component in the navigation stack (HomePage/SecondPage).

  3. pop

    Example:

    this.navCtrl.pop();

    (or)

    this.navCtrl.pop(); //DeepLink navigation

    Consider you are in secondpage now and wanted to go back to the previous page which is home page. Then just use the above example it will pop one component from the navigation stack and gives you only the homepage component in the navigation stack.

like image 138
Mohan Gopi Avatar answered Nov 12 '22 03:11

Mohan Gopi


You can use Ionic deep links for that.

Example from the doc:

@IonicPage({
  name: 'my-page',
  segment: 'some-path'
})

When navigating to this page as the first page in the app, the URL will look something like this:

http://localhost:8101/#/some-path

Good article about it: Link to Pages via URLs with Deep Linking in Ionic

like image 2
Sampath Avatar answered Nov 12 '22 03:11

Sampath