最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - First time and during refresh only API call should call and give response - Stack Overflow

programmeradmin2浏览0评论

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
  • This is what angular.dev/api/router/Resolve is for. It will run on the first time the component (Home page) is initialised and never again in the session (unless you tell it to with 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
Add a comment  | 

1 Answer 1

Reset to default 1

First 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);
    }
  );
}
发布评论

评论列表(0)

  1. 暂无评论