0

I'm trying to kickstart my app by providing it a client id by using routes:

www.mysite.com/11298/[any page]

11298 would be the client id and user can navigate to any page under that route. So basically the clientId will always be in the address, whatever the subroute is. Here's the router config:

export const ROUTES: Routes = [
  {
    path: ':clientId',
    canActivate: [InitializeGuard],
    component: StartPageComponent,
    children: [
      { path: '', redirectTo: 'page1', pathMatch: 'full' },
      { path: 'page1', component: Page1Component },
      { path: 'page2', component: Page2Component },
      { path: 'page3', component: Page3Component },
      { path: 'page4', component: Page4Component },
    ],
  },
];

I'm currently using InitializeGuard on the routes and it would be ideal if I could access the :clientId from that service. Is this possible? If the user would always navigate to www.mysite.com/14918, I could just read the id in StartPageComponent, but I can't know which route the user will go to.

How could I read the :clientId where ever the user goes to without rewriting some subscription code into every component in my app? It would be really cool if I could pass the :clientId to InitializeGuard for init and it would take care of it non-intrusively.

If it's not possible, what would be the another way of doing this? I'm thinking about a shared service for init, but I would still have to do the init in all of the PageXComponents, which doesn't sound good.

If this doesn't work as a strategy, what would be a good strategy to pass a site-wide clientId to every route upon navigation?

  • Since Angular will re-use components when the client ID changes, I don't see how you can set your child route components to not have a subscription to some change in that. Even if you have a service that is provided to each of them, they would simply subscribe to some method in that class that provides the client ID. Angular already provides this on the `ActivatedRoute`, so there is no reason to create another class to provide this functionality. – Daniel W Strimpel Apr 29 '18 at 05:06
  • Hmm, I understand. Would there be a smarter way to pass an app wide parameter that would only need to be read once? Like... pass query parameter to the index.html before the app starts? Or something that doesn’t require a specific launcher component but is able to preserve the access path after intializing, do it on the go? – Juhana Pietari Lehtiniemi Apr 29 '18 at 09:42
  • When the users share a link to the site for example on social media, the id would have to be part of the url. Otherwise the app would know what template to use. This is why I thought i’d uaing the route since it would stick throughout the browsing in the url and get shared. – Juhana Pietari Lehtiniemi Apr 29 '18 at 09:45

0 Answers0