I am trying to navigate to a specific url using location.go service from typescript function. It changes the url in the browser but the component of the url is not reflected in the screen. It stays on the login (actual) screen - say for eg:
constructor(location: Location, public _userdetails: userdetails){
this.location = location;
}
login(){
if (this.username && this.password){
this._userdetails.username = this.username;
this.location.go('/home');
}
else{
console.log('Cannot be blank');
}
}
Is there a compile method or a refresh method I am missing?
Yes, for redirecting from one route to another you should not be using
location.go
, it generally used to getnormalized url
on browser.Location docs has strictly mentioned below note.
Rather you should use
Router
API'snavigate
method, which will take['routeName']
as you do it while creatinghref
using[routerLink]
directive.If you wanted redirect via URL only then you could use
navigateByUrl
which takes URL as string likerouter.navigateByUrl(url)
Update
In further study, I found that, when you call
navigate
, basically it does acceptrouteName
inside array, and if parameters are there then should get pass with it like['routeName', {id: 1, name: 'Test'}]
.Below is API of
navigate
function ofRouter
.When
linkParams
passed togenerate
function, it does return out all the Instruction's required to navigate on other compoent. HereInstruction
meansComponentInstruction
's which have all information about Routing, basically what ever we register inside@RouteConfig
, will get added toRouteRegistry
(like on whatpath
whichComponent
should assign to torouter-outlet
).Now retrieved Instruction's gets passed to
navigateByInstruction
method, which is responsible to load Component and will make various thing available to component likeurlParams
& parent & child component instruction, if they are there.Instruction (in console)