Ionic2 Deep linking segment optional params

Viewed 704

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 ?

1 Answers

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.

Related