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' }
}]
},
Jayraj ChhayaPosted May 3, 2024, 5:58 AM
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:
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:Naimish MakwanaPosted May 3, 2024, 4:53 AM
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:
And in your HTML, you can keep the routerLink as it is:
Thanks
Jignesh KumarPosted May 3, 2024, 4:15 AM
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