spring - Jhipster - Angular HTTP Get 请求从未执行
问题描述
我在 Web 项目中使用 JHipster 框架。我是新手,所以我有一些问题。我尝试实现从按钮到后端的 API 调用。我想要该按钮Resolve
,当它被单击以调用一个方法时,该方法会转到服务以进行实际的 API 调用。现在有问题了。当我在我的服务中访问 resolveFile 方法时,我会收到所有警报,但不会收到 API 调用。当我尝试直接访问从 Postman 拨打电话的后端时,它运行良好。问题似乎出在myFile.service.ts文件中。你有什么建议吗?
myFile.component.html:
<button (click)="resolveFile(myfile.id)" class="btn-dark btn btn-sm">
<span class="fa fa-beer"></span>
<span class="d-none d-md-inline" >Resolve</span>
</button>
myFile.component.ts:
resolveFile(id) {
alert('mpika1');
this.myfileService.resolveFile(id);
}
我的文件.service.ts
private resourceUrl = SERVER_API_URL + 'api/my-file/resolve';
resolveFile(id: String): Observable<EntityResponseType> {
debugger;
alert();
this.http.get(`${this.resourceUrl}/${id}`);
alert();
return null;
}
我的文件资源.java
@GetMapping("/my-file/resolve/{id}")
public ResponseEntity<MyFileDTO> resolveFile(@PathVariable String id)
{
log.debug("REST request to resolve myFile: {}", id);
// TODO
myService.resolveFile(id);
return null;
}
解决方案
你必须订阅它。
this.http.get(`${this.resourceUrl}/${id}`).subscribe((res) =>{
//res -> response output
});
Angular 使用名为rxjs的库,更多信息https://angular.io/guide/rx-library
为了更好的实现,最好返回Observable
而不是返回null
我的文件.service.ts
private resourceUrl = SERVER_API_URL + 'api/my-file/resolve';
resolveFile(id: String): Observable<EntityResponseType> {
return this.http.get(`${this.resourceUrl}/${id}`);
}
myFile.component.ts
resolveFile(id) {
//In here you could wait the response
this.myfileService.resolveFile(id).subscribe((res) =>{
//res -> response output
alert('mpika1');
});;
}
推荐阅读
- javascript - 身份验证和文件请求
- jenkins - 如何使用 puppet 在 jenkins 中添加工作?
- bash - 如何通过 ssh 从远程服务器运行期望脚本?
- phpmailer - 2019 无法调用未创建 PHPMailer 版本 6.0.7 实例的新 PHPMailer
- c# - 带有单选按钮的自定义列表视图。我需要从多个单选按钮列表中选择单选按钮
- java - 为什么我的应用程序不显示录制的消息?
- java - 在java中扩展多个类的泛型
- angular - Angular:返回符合特定条件的对象
- rust - 什么是 futures::future::lazy?
- laravel - 如何将外部 vue.js 库导入 laravel / vue.js 应用程序?