I am facing a strange issue in my Angular 4 project. I am trying to navigate to a path through code (on button click). I am using router.navigate()
method as follows to accomplish the job --
this._router.navigate(["/employeeDetails", selectedEmployee.EmployeeId]);
Where selectedEmployee.EmployeeId
is a number.
The navigation happens but I am getting a very weird thing in the URL. Initially the URL shows as follows --
http://localhost:4200/?employeeDetails/170
and then the ?
sign vanishes from the URL and the required page is loaded.
Can anyone please tell me why the ?
sign is coming in the URL. Ideally it should load the respective component for the route "/employeeDetails"
without refreshing the page. I also tried the code without /
as follows but no help.
this._router.navigate(["/employeeDetails", selectedEmployee.EmployeeId]);
Any help would be appreciated.
In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes.
The query parameters feature in Angular lets you pass the optional parameters while navigating from one route to another.
navigateByUrl() The Angular 10 Router provides two methods that you can use to navigate to other components in your component class instead of using the RouterLink directive in the template.
The Router-Outlet is a directive that's available from the router library where the Router inserts the component that gets matched based on the current browser's URL. You can add multiple outlets in your Angular application which enables you to implement advanced routing scenarios.
None of the previous answer gives the correct answer even thought some of them work! But all put a patch without acting on the cause of the event we have.
The solution is to set the type="button" to the button element, because by default the form identifies the button as type="submit" if not specified and triggers the submit action.
However thanks because the combination of the answers helped me to find the real cause!
To have que question mark in URL with angular 2 (4,5,6) for parameters:
this.router.navigate(['/companie'], { queryParams: { type: 'firstRegister' }});
you will get:
https://myurl/#/companie?type=firstRegister
To have the classic parameter, it is:
this.router.navigate(['/companie', { type: 'firstRegister' }])
you will get:
https://myurl/#/companie;type=firstRegister
Reason could be a missing action="/formsubmit"
. When Chrome tries to send a GET request without it this it appends a ?
to the current URL.
Try the below for your submit action:
onSubmitAction(event){
event.preventDefault()
}
Hope it helps :)
I had the same problem. Solved it by adding this action to my form tag:
<form action="javascript:void(0)"
Be sure that your injected _router: Router
in your components constructor like this:
constructor(private _router: Router) {}
Change this:
this._router.navigate(["/employeeDetails", selectedEmployee.EmployeeId]);
to this:
this._router.navigate(['/employeeDetails', selectedEmployee.EmployeeId]);
or this:
this._router.navigate(['/employeeDetails/' + selectedEmployee.EmployeeId]);
In your routing file you should have something like this:
const routes: Routes = [
...
{ path: 'employeeDetails/:id', component: YourComponent },
...
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With