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