API call should get called in home page during first time page load and during refresh, If navigate from other page to home page, API response should not get update. give some idea how to handle this
I have writing the code like below, and it is not working as expected
Thanks
Example in homeponent.ts
ngOnInit() {
this.Service.IpnValue().then(ipn => this.userIpn = ipn);
this.getLoggedOnTime(this.Ipn);
}
getLoggedOnTime(Ipn: string): void {
this.ervice.getUserLoggedOn(Ipn).subscribe(
(response) => {
this.lastLoggedOnTime=response.code;
console.log('User logged on:', this.lastLoggedOnTime);
},
(error) => {
console.error('Error logging on user:', error);
}
);
}
API call should get called in home page during first time page load and during refresh, If navigate from other page to home page, API response should not get update. give some idea how to handle this
I have writing the code like below, and it is not working as expected
Thanks
Example in homeponent.ts
ngOnInit() {
this.Service.IpnValue().then(ipn => this.userIpn = ipn);
this.getLoggedOnTime(this.Ipn);
}
getLoggedOnTime(Ipn: string): void {
this.ervice.getUserLoggedOn(Ipn).subscribe(
(response) => {
this.lastLoggedOnTime=response.code;
console.log('User logged on:', this.lastLoggedOnTime);
},
(error) => {
console.error('Error logging on user:', error);
}
);
}
Share
Improve this question
edited Feb 10 at 18:18
Barremian
31.1k4 gold badges35 silver badges64 bronze badges
asked Feb 10 at 10:10
web_inweb_in
1336 bronze badges
1
|
1 Answer
Reset to default 1First create a property on the service that stores this lastLoggedOnTime
(property that should be called only on load).
Then on first load/refresh only, this property will be undefined
use this to trigger the API call like below:
ngOnInit() {
if(this.Service.lastLoggedOnTime === undefined) {
this.Service.IpnValue().then(ipn => this.userIpn = ipn);
this.getLoggedOnTime(this.Ipn);
}
}
getLoggedOnTime(Ipn: string): void {
this.Service.getUserLoggedOn(Ipn).subscribe(
(response) => {
this.lastLoggedOnTime=response.code;
console.log('User logged on:', this.lastLoggedOnTime);
},
(error) => {
console.error('Error logging on user:', error);
}
);
}
runGuardsAndResolvers: 'always'
). That's one way. Another would be to look at adding something like storing a JWT token to the session / local storage and get your service to validate the token which doesn't require an API call and is a pretty cheap operation. – Edward Newsome Commented Feb 11 at 11:58