Ionic2 Deep linking segment optional params
Asked Answered
S

1

8

I cannot find a single document that explains how to use optional params in segments.

In my app.module.ts I have this deepLinkConfig :

let deepLinkConfig = {
  links: [{
    component: HomePage, name: 'Home', segment: 'home'
  }, {
    component: ConnexionPage, name: 'Connexion', segment: 'connexion'
  }, {
    component: InscriptionPage, name: 'Inscription', segment: 'inscription'
  }, {
    component: ProfilePage, name: 'Profile', segment: 'profile/:userId'
  }, {
    component: ExplorePage, name: 'Explore', segment: 'explore'
  }, {
    component: FAQPage, name: 'FAQ', segment: 'faq'
  }]
};

...
imports: [
    ...
    IonicModule.forRoot(MyApp, {}, deepLinkConfig);
],

The user should be able to access his profile via /profile, but also consult another user's profile with /profile/:userId. But when I try to access /profile, I get an error :

Uncaught (in promise): invalid views to insert

Via navCtrl, it's possible to go to 'profile' with an empty string as the userId, but once you actualize the page, it goes blank.

Is there a way to make the params optional in deeplinking ?

Sixtieth answered 7/8, 2017 at 9:47 Comment(2)
are subscribe to Deeplinks.routeWithNavController in appComponent? if not, here you can see how to do that github.com/ionicthemes/…Filly
No I didn't have to since I used another method. Edited OP so you can see what I did.Sixtieth
U
0

This question has also been asked here I think.

I know this is not so much of a solution I propose, but the only option that seem available at the moment will be to use a dummy value for the optional parameter, and to handle that in your code.
For example, the user would access it's own profile at /profile/- and other profiles at /profile/anyOtherUser.

Unfinished answered 24/12, 2018 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.