How to achieve angular2 nested component routing without the additional slash in url path

Jonathan Miles

I'm building a single page application using angular2 and it needs to render a different application template dependent on the app's routing. At the highest level it should be able to switch between the main applications template and the app's admin template.

Routing Rules:

  • If the url path starts with /auth then it's handled by the AuthApp
  • If it starts with anything else then should be handled by the MainApp

Implementation:

In order to achieve this I have a component app at the highest level which delegates off responsibility to either the AuthApp or MainApp, like this.

@RouteConfig([
  {
    path: '/auth/...',
    component: AuthApp,
    as: 'Auth'
  },
  {
    path: '/...',
    component: MainApp,
    as: 'Main'
  }
])

Then each sub-app (e.g. AuthApp, MainApp) has it's own route definitions such as this.

@RouteConfig([
  { path: '/current-vacancies', CurrentVacancies, as: 'CurrentVacancies', useAsDefault: true },
  { path: '/candidates/create', CandidateCreate, as: 'CandidateCreate'},
  ...
])

Functionally and from a design point of view this works great!

Problem:

In the main app, where the application's route config doesn't specify a path (e.g. /... => MainApp) I do not want a nested url path. For example, if you navigate via the app (routerLink) to CurrentVacancies it updates the url and prepends an extra unwanted forward slash. The url path now reads like this .../#//current-vacancies when I want it to read .../#/current-vacancies.

The application routing actual works when you type .../#/current-vacancies in the address bar, it transitions correctly. Yet navigating via the applications routerLink directive prepends the addition unwanted forward slash.

I understand logically why this is happening, the root component which delegates off to MainApp has a path of '/' and it's concatenated together with it's child components to form a full url path. BUT in this instance it's not desirable to have the additional slash. Any ideas how I can remove it, or somehow override this behaviour.

Btw, I've considered moving the MainApp component routing to the top most level and although this does resolve the url path issues I'm reporting, it does not allow me to switch templates at the top-level.

In summary

Please help me with, either

  • how to remove the unwanted / on main app routes "preferred option", or
  • how design app to have switchable top-level templates with nested component routing.

Thanks

Cobus Kruger

This is working today (Angular 4) and seems to have been fixed before Angular 2 final was released.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular2 routing with nested states

Angular 2: how to get params from URL without using Routing?

How to set application context path in angular2 routing properly?

How to achieve multi tenant routing in angular?

Angular2 routing without component

Angular 2 routing removes URL path

How to display a seperate html page without using a component for routing in angular 2?

How to achieve nested URL for OneToOne Relationship?

how to replace routing path in Angular

how to make nginx location match path both with slash and without slash?

Angular 8 routing, cant navigate to component without a name before slash

Angular routing : how can I redirect all unknown nested routes to one component?

How to make jboss accept %2F encoded slash in URL path

Nested Routing in angular2

Angular2 IE adds additional slashes to URL after base path

Routing programmatically to other component using relative path in Angular2/4

angular2 route parameter with additional url

Angular routing - redirect url to component

Angular nested component routing

How to routing directly to the component that I want, without going to the main component

How to add trailing slash in URL in angular 7 without 404 issue on refresh?

Angular 7 component redirection without routing

Laravel routing : use comma without slash

Canonical URL without trailing slash for file/path

Angular routing loading the grandchild component without the child

How to use / (slash), without using it as a path in React routing?

Path without leading slash

How to get pwd relative path in bash without first slash

Angular problem with nested routing, displaying child component above parent component