gpt4 book ai didi

angular - 检查 api 是否从 Angular 客户端应用程序关闭

转载 作者:行者123 更新时间:2023-12-05 06:23:23 26 4
gpt4 key购买 nike

我有一个 Angular 8 应用程序,它从 .Net 代码 Web API 应用程序中提取数据。我需要检查 API 是否已关闭然后重定向到系统维护页面。我目前已经编写了一个方法,该方法将调用端点以查看服务是否可用并订阅该方法来检查每 10 秒。这是最好的方法吗

连接服务

@Injectable()
export class ConnectionService {
constructor(private httpClient: HttpClient, private configurationService: ConfigurationService) { }
checkIfCoreApiIsAvailable() {
var pingUrl = this.configurationService.baseUrl + "/api/online";
return this.httpClient.get(pingUrl);
}
}

离线组件

@Component({
selector: 'not-found',
templateUrl: './offline.component.html',
styleUrls: ['./offline.component.css'],
animations: [fadeInOut]
})
export class OfflineComponent implements OnInit {
subscription: Subscription;
pingInterval: number;
constructor(private connectionService: ConnectionService) {
// Check if Core-API service is Online or Offline
this.pingInterval = environment.corePingIntervalSeconds;
const source = interval(this.pingInterval*1000);
this.subscription = source.subscribe(val =>
{
this.opensnack();
console.log(val);
});
}

最佳答案

这种检查服务器可用性的方法完全没问题。您错过了对您的服务的调用。我认为这个重构会让你到达那里。 Here is a Stackblitz with working example ,我 ping 了谷歌,但你明白了:

      import { first } from "rxjs/operators";

export class OfflineComponent implements OnInit {

private source = interval(
environment.corePingIntervalSeconds * 1000;
);

constructor(private connectionService: ConnectionService) {

this.source.subscribe(() => {
this.connectionService
.checkIfCoreApiIsAvailable()
.pipe(first())
.subscribe(resp => {
if (resp.status === 200 ) {
console.log(true)
} else {
console.log(false)
}
}, err => console.log(err));
});
}
...

关于angular - 检查 api 是否从 Angular 客户端应用程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58413564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com