第一次和刷新期间,只有API呼叫应打电话并给予响应

问题描述 投票:0回答:1

API呼叫应在第一次页面加载期间和刷新期间在主页上打电话, 如果从其他页面导航到主页,则API响应不应获得更新。给一些想法如何处理这个

我已经像下面一样编写代码,并且它的工作原理不如预期

thanks

示例 在

home.component.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); } ); }
    
angular angularjs user-interface web frontend
1个回答
0
投票
lastLoggedOnTime

(仅在加载上应调用的属性)。 then仅在第一次加载/刷新时,此属性将用于

undefined
使用此属性触发API调用,如下:

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); } ); }

    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.