angular - 如何通过在拦截器中调用 API 服务方法来避免无限次 API 命中
问题描述
我有一个带有 API 服务方法的组件
TS
showAcknowledgement() {
this.noticeService.getNotice(ConstUrls.NoticeManagement.GetUserNotice).subscribe((data: any) => {
data.data.filter((res: any) => {
if (data.isSuccessful === true) {
this.noticeText = res.noticeText
this.noticeAttachment = res.noticeAttachement,
this.noticeCode = res.noticeCode
}
})
});
}
我需要将此服务方法调用到 Interceptor:
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
tap((resp) => {
if (resp instanceof HttpResponse && resp.body.isNotificationAvailable) {
this.showAcknowledgement();
}
})
);
}
解决方案
推荐阅读
- vue.js - 为什么 Vue 无法识别 v-html 属性中使用的数据成员?
- javascript - 我正在尝试用 Javascript 制作计算器。但它不工作
- java - 使用 maven-compiler-plugin 从编译中排除 src/main/java
- jenkins - ZAP 扫描:Jenkins 作业失败 (url_not_in_context)
- c# - EF Core 自定义复杂约束
- javascript - React-native 复选框逻辑
- flutter - Flutter Create Dropdown 月/年选择器
- javascript - 文件、javascript、xml、对象FormData的链接问题
- scala - Scala Spark 处理带逗号的单引号字符
- javascript - 如何在 JavaScript 中将 Promise 链接在一起