#Regarding paths in routes

44 messages · Page 1 of 1 (latest)

potent veldt
#

I am following a tutorial and this is the code it shows:

Questions:
Do I really need the first option? Seems like the last one would match an empty string too.

Tutorial says pathMatch: full is needed on last one or else there'd be endless redirects, upon testing, endless redirects don't happen without that config

#

or perhaps I'm not noticing the endless redirect, not sure

timber pollen
#

Generally path: '' without pathMatch: 'full' is an error

potent veldt
#

I was thinking that because the docs say it matches from left to right and as soon as it finds a match it goes there

#

how come other paths do work for me though? i'd expect them to always take me to home page

timber pollen
#

Right now, every path matches the first HomeComponent. And you don't have a redirect (visible in your code) so I don't know how a redirect could happen.

#

g!badeyes @potent veldt Post code as text please

sour stormBOT
#

@potent veldt Please do not post pictures of code (especially photos of a physical screen) as they are difficult to read and difficult to correct (as you cannot copy code from a screenshot to modify it).

potent veldt
#

noted

#

if I put any path not on there it is taking me to home component

timber pollen
#

Angular Router's path matching is a little smarter than it claims. But still you should be clear about what you want to have. Which means properly decorating and ordering your routers.

potent veldt
#

that is what the last option is for according to the tuto

timber pollen
#

There is no point in putting pathMatch: 'full' on path: '**'

#

g!toh @potent veldt Maybe you should do the official tutorial instead of one that is wrong or confusing you.

sour stormBOT
#

@potent veldt The place we recommend starting your Angular journey is with the Tour of Heroes tutorial, located here https://angular.io/tutorial.

potent veldt
#

thanks I completed that a while back

#

when would endless redirects actually happen?

timber pollen
#

Post the routes as text please

#

There are no redirects in your code. No redirects happen at all

potent veldt
#
const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'members', component: MemberListComponent },
  { path: 'members/:id', component: MemberDetailComponent },
  { path: 'lists', component: ListsComponent },
  { path: 'messages', component: MessagesComponent },
  { path: '**', component: HomeComponent, pathMatch: 'full' },
];
#

got it

timber pollen
#

There are no redirects in your code. No redirects happen at all
Unless you've done something terrible in your code.

potent veldt
#

so am I correct that the first option I can get rid of?

timber pollen
#
const routes: Routes = [
  { path: 'lists', component: ListsComponent },
  { path: 'members', component: MemberListComponent },
  { path: 'members/:id', component: MemberDetailComponent },
  { path: 'messages', component: MessagesComponent },
  { path: '', pathMatch: 'full', component: HomeComponent },
  { path: '**', redirectTo: '/' },
];
potent veldt
#
const routes: Routes = [
  // { path: '', component: HomeComponent },
  { path: 'members', component: MemberListComponent },
  { path: 'members/:id', component: MemberDetailComponent },
  { path: 'lists', component: ListsComponent },
  { path: 'messages', component: MessagesComponent },
  { path: '**', component: HomeComponent /*, pathMatch: 'full' */ },
];
timber pollen
#

This would be the typical way to set that up ☝️ But what you are doing is non-standard.

#
const routes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: 'lists', component: ListsComponent },
  { path: 'members', component: MemberListComponent },
  { path: 'members/:id', component: MemberDetailComponent },
  { path: 'messages', component: MessagesComponent },
  { path: '', pathMatch: 'full', redirectTo: '/home' },
  { path: '**', component: PageNotFoundComponent },
];
timber pollen
potent veldt
#

ah I see, I was just looking at tour of heroes

#

what would be "standard" then

#

yours looks like the tour of heroes example

#
const appRoutes: Routes = [
  { path: 'crisis-center', component: CrisisListComponent },
  // { path: 'heroes',     component: HeroListComponent }, // <-- delete this line
  { path: '',   redirectTo: '/heroes', pathMatch: 'full' },
  { path: '**', component: PageNotFoundComponent }
];
timber pollen
potent veldt
#

oh I see, when you said "if you want to be different" you meant my post not yours right above your post. So your post is the standard way correct?

timber pollen
potent veldt
#

Got it. Sorry when I click on the link it doesn't paticularly highlight the message. I guess it just centers it.

#

thank you

#

Now I'm hesitant to continue this tutorial. It's a dotnet angular tutorial. Can you recommend any tutorials?

timber pollen
potent veldt
timber pollen
potent veldt
#

Ok. Well I'll just review docs and compare every now and then.