Reputation: 303
Im being asked to do a website in angular and accept direct parameters for a client at the URL level.
For example, a sales agent will have a website like www.website.com/agent/2021 and customize the site based on his ID (2021 in this case). I made this work just fine by setting this at routing.module:
{ path: '', component: AgentComponent, pathMatch: 'full' },
{ path: '**', component: AgentComponent },
Where
path: 'agent/:code', component: AgentComponent,
It all works perfect, except than when I browse www.website.com straight, it shows a blank page. This is fine, because the code at app.component.ts runs first. Ive setup an agent ID 1 at the database so when someone browses the URL straight (without a route), I can route to www.website.com/agent/1 but at this point, I cannot figure out if the user accessed the direct URL, or a URL containing a route (such as www.website.com/agent/2021)
Is there a solution to detect if the client went straight to the URL (www.website.com) and in such case, redirect to www.website.com/agent/1, or if the client is coming from a direct route (www.website.com/agent/2021), keep him on that route?
Thanks.
Upvotes: 2
Views: 459
Reputation: 1496
You have it reversed.
{ path: '**', redirectTo:'agent/1' },
{ path: '', component: AgentComponent },
Upvotes: 4