4

I have a routing module as follows:

App-routing module

const routes: Routes = [
  {
    path: '',
    redirectTo: environment.devRedirect,
    pathMatch: 'full',
    canActivate: [AuthenticationGuard]
  },
  {
    path: 'customers/:customerId/contracts/:contractId',
    loadChildren: './grouping/grouping-routing.module#GroupingRoutingModule'
    canActivate: [AuthenticationGuard],
  }
];

And a child component with routes:

const routes: Routes = [
      {
        path: 'create',
        component: CreateEditComponent,
        data: { animation: 'create' },
        canActivate: [ AuthenticationGuard ]
      },
      {
        path: ':groupId/edit',
        component: CreateEditComponent,
        data: { animation: 'edit' },
        canActivate: [ AuthenticationGuard ]
      },
      {
        path: '',
        component: OverviewComponent,
        data: { animation: 'overview' },
        canActivate: [ AuthenticationGuard ]
      }
];

I have a top level navbar component which sits in the app.component.html.

Both the navbar component and the CreateEditComponent have a function that looks like below. Both are called using a button with (click):

  goToOverview(): void {
    this._router.navigate(['customers/:customerId/contracts/:contractId']);
  }

When I debug the router object both look exactly the same i.e. have all the same paths etc.

My problem is that the navbar function routes correctly but the CreateEditComponent navigates, appends a ? then reloads the page. What am I missing here and why do two seemingly similar calls have such different results when the activatedRoute objects are identical?

2 Answers 2

6

Finally figured out what was causing the refresh. The button with the (click) handler that was causing the refresh was sitting inside a <form> tag. Whenever the click function was called which did the router.navigate it caused a form submission which seems to be the cause

Sign up to request clarification or add additional context in comments.

1 Comment

moved he button out of the form. Could also be resolved by either removing ngSubmit from the form or adding it and removing the click handler from the button. Both methods the button is inside the form tags
2

you're using a relative URL, which means you're navigating relative to the current components position in the router tree.

Navbar is in the root component, so it is navigating relative to the router root which is "/".

CreateEditComponent is a routed child of root, so it is navigating relative to the the routers first child, which may be "/create" or whatever, I'm not sure how exactly you have your router structured from the question. But basically, everytime you nest a router outlet / add child routes, you create a new node in the router tree.

The important part though is, doing it like this:

 this._router.navigate(['/customers/:customerId/contracts/:contractId']);

with a "/" in front will make it an absolute URL that will go to the same place no matter where it's called from as it will always navigate from the root.

You're probably seeing the odd refresh behavior because you're trying to navigate to an invalid route and causing an error that's being handled poorly.

My personal opinion, FWIW, nested router outlets are cool and all, and they provide a substantial amount of power, but they also create a lot of complexity in your application. Use them as appropriate, but be judicious in their use and tend towards a flatter structure when possible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.