My angular navigation router url renders with localhost/books%2Ftitle instead of '/'
Here is my routerLink. Parent routes working well. Child route not working.
Html:
[routerLink]="['/', path]"
Router config:
path: 'books',
component: BooksComponent,
children: [
{
path: 'books',
component: BooksComponent,
data: { text: 'New book' }
},
{
path: 'books/title',
component: EditBookComponent,
data: { text: 'Edit book' }
}]
},

Jignesh KumarRank #91 · 22.9K reputationPosted May 3, 2024, 12:15 AM EDT
Hello maniknandan,
%2F is URL-encode for / in web broser url. it's not an issue. you need to check other points if you are facing issue
Naimish MakwanaRank #324 · 7.7K reputationPosted May 3, 2024, 12:53 AM EDT
It seems like there is a small mistake in your router configuration. The issue is with the paths you’ve defined for your child routes. They should be relative to the parent route, not absolute.
Here’s how you can correct it:
{ path: 'books', component: BooksComponent, children: [ { path: '', // This matches the parent route '/books' component: BooksComponent, data: { text: 'New book' } }, { path: 'title', // This matches the child route '/books/title' component: EditBookComponent, data: { text: 'Edit book' } } ] }And in your HTML, you can keep the routerLink as it is:
Thanks
Jayraj ChhayaRank #304 · 8.2K reputationPosted May 3, 2024, 1:58 AM EDT
When using the
[routerLink]directive in Angular, the issue with the URL rendering%2Finstead of/can be resolved by ensuring that the path segments are correctly defined in the router configuration. In your case, the issue might be related to the way the child routes are defined.To fix this issue, make sure that the child routes are defined without duplicating the parent route path. In your router configuration, the child route path should be relative to the parent route. Here's an example of how you can adjust the child route path:
{ path: 'title', component: EditBookComponent, data: { text: 'Edit book' } }By defining the child route path as
'title'instead of'books/title', the navigation URL should render correctly aslocalhost/books/titlewithout the%2Fsymbol. Ensure that the routerLink in your HTML template reflects this change: