Using URL's to navigate in Ionic 2


#1

I want to use URL’s to navigate between pages in an Ionic 2 project. I am aware that the new navigation system works with push and pop but the docs clearly mention that URL’s can still be used. I tried searching the docs and the source on how to do this but I couldn’t find anything.

If anyone has managed to do this or can figure it out please let me know.

Like native apps, URLs are not required for navigation. Instead, pages are pushed and popped on and off a navigation controller’s page stack. URLs can be used to link back to unique, important parts of your app (“deeplinking”) but don’t define navigation as your app runs.

When it comes to URLs, Ionic 2 works a bit differently than Ionic 1. Instead of using URLs to navigate, we use them to make sure we can always come back to a page (on app launch, for example). This means we aren’t not limited to using href to navigate around. However, we still have the option to use a URL to navigate to a page when necessary.

http://ionicframework.com/docs/v2/getting-started/tutorial/navigation/


#2

Check this Angular 2 routing example , May be can help you http://www.codeandyou.com/2015/11/understand-routing-in-angular-2.html


#3

Have you figured this out?


#4

Any news here? I’m also interested in using URLs.


#5

Im also very interested in using URLS. Can’t make the Angular 2 routing work.


#6

I would also love to see navigation by URL, but also by the normal pushing and poping and passing of nav params. The ionic 2 navigation and nav param passing has been working great for me, but I can foresee when a url would be useful as well, such as when/if the app gets refreshed. Without a url, memory is cleared and all nav params to the @page you werw in are lost. Ideally, using nav.push with nav params would also encode the params into the URL. Those are my thoughts.


#7

We will add some examples for this when Angular finishes some changes to their routing. See this comment: https://github.com/driftyco/ionic/issues/5479#issuecomment-185267794

and this sample code: https://github.com/driftyco/ionic/blob/2.0/ionic/components/nav/test/routing/index.ts#L47


#8

Ye, that issue was mine. But I didn’t get it to work when I only want to use URL routing. I rather not keep the location stored in localstorage because refresh the page has to work :slight_smile: some info would be nice even if we have to change it later.

Thanks! :slight_smile:


#9

Hi @brandyshea,

I’m looking to setup deep linking on push notifications through ionic.io for my Ionic 2 app. Do you have any samples on how this is possible? Any advice is much appreciated. Basically, when a user taps on a notification, it brings the user to the right page in the app.

Thanks.


#10

From developer’s point of view the current state of the art (stack for navigation, no explicit url’s) is uncomfortable for several reasons:

  • you cannot share link to some particular screen with your co-workers or paste it to issue tracker.
  • on code change, live-reload will throw you to the default starting page. If the page you currently develop is somewhere far away, is is annoying to click you way throught the app after every livereload.
  • you cannot easily run tests which should start on some particular screen

Also the stacked navigation may guide developers to create UX-unfriendly navigation in their apps.
Most mobile apps have tree-like navigation structure – i.e. there is a root page (e.g. Dashboard), a side menu, and you can dive to several master-detail pages, but typically you don’t dive more than 1 or 2 levels of depth (I would say developer should avoid more than 2 levels of depth of navigation, because the user will get lost).
Unfortunately stack navigation may suggest to (newbie) developers that navigation into depth is a good thing and instead of prefering tree-like navigation, developer will use stack to dive deeper and deeper.


#11

Is there any news on this topic? There are various requirements and project setups that make deep-linking and a combination of url-based routing and push/pop-based navigation necessary. It would be be really helpful, if there was was a working example or best practice available.


#12

Angular has refactored their router multiple times, and it is not currently supported in the latest version of Ionic. We added the following section to our changelog with the beta.7 release:

The Angular router is currently under heavy development and refactoring. As a result of this, Angular’s router is currently disabled within Ionic. If your app requires use of the router we recommend waiting until a future release of Ionic when Angular has completed work on the new router. However, this does not affect Ionic’s navigation system and it continues to work with the same API from previous versions.

We understand and agree that routing is important, and we will add support for it when Angular has released a stable version. Thank you and sorry!


#13

It means right now there is no way to share a link for a particular page? Also I’d like to see URL accept parameter as well like http://a.com/pagename&id=10


#14

Do you know an estimate on when it is going to be done?


#15

A stable angular version with the component router has released. When do you plan to integrate it?


#16

I would also love to know what the options / plans are for this. Don’t want to delve deep into the Angular Router if this will become a feature, before getting too far into my current app. URL navigation will be critical for it.


#17

Actually just found this:

Great if you are working on a PWA like me! And it sounds like it will be in the docs soon.


#18

Does the latest build support url routing ? It’s really painful to go back to the home page in the live reload every time my code changes…


#19

Oh, just noticed there is a deep linker to use here:

Thanks


#20

It seems straightforward but I got “invalid page component: Null” with below. Idea?

http://localhost:8100/#/housedetail/1111

export const deepLinkConfig: DeepLinkConfig = {
links: [
{ component: TabsPage, name: ‘home’, segment: ‘home’ },
{ component: HouseDetailPage, name: ‘detail’, segment: ‘housedetail/:id/:list’, defaultHistory: [ TabsPage ] }
]
};

imports: [
IonicModule.forRoot(MapleApp, {

},deepLinkConfig),
CloudModule.forRoot(cloudSettings),

],