html - Angular:在资产文件夹发生更改时防止自动重新编译
问题描述
我正在尝试在 angular 项目的资产中下载从 spring boot 生成的文件。当我从 Angular 服务调用 Spring API 时,Angular CLI 在资产 Angular 文件夹中创建文件后重新编译项目,然后在从 Spring Boot API 获取响应之前重新加载页面。
我尝试以多种方式从角度调用spring boot API:
- 在我的组件的 ngOnInit() 中调用 api
- 在我的组件的构造函数中调用 api
- 在单独的函数中调用 api
- 在下载功能中使用异步等待
我不知道如何继续
弹簧靴
@GetMapping(path = "/downloads/{fileId}", produces = "application/json")
@ResponseBody
public ResponseEntity<String> download(@PathVariable String fileId){
Gson gson = createGson();
List<com.bioimis.blp.entity.File> fileById;
try {
fileById = fileRepository.findFileById(fileId);
} catch (Exception e) {
logger.error(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
if (fileById.isEmpty()) {
logger.error(fileId + " not found");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
if(fileById.get(0).getDeletionDate() != null) {
List<String> phrases = new ArrayList<>();
try {
phrases = translationRepository.getAllTranslationsByFileId(fileById.get(0).getId());
} catch (Exception e) {
logger.error(e.getMessage());
return ResponseEntity.status(HttpStatus.OK).body(null);
}
String file = "";
for (int i = 0; i < phrases.size(); i++) {
file = file.concat(phrases.get(i));
}
file = file.concat("\0");
/*Suppongo che prima dell'estensione gli ultimi 5 caratteri del file sono in formato 'languageCode_countryCode'*/
String nameFile = fileById.get(0).getPath().split("/")[fileById.get(0).getPath().split("/").length - 1];
Language language;
try {
language = languageRepository.findLanguageById(fileById.get(0).getLanguageId()).get(0);
} catch (Exception e) {
logger.error(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
int i = StringUtils.lastIndexOf(nameFile, '.');
String ext = StringUtils.substringAfter(nameFile, ".");
nameFile = nameFile.substring(0, i - 5) + language.getLanguageCode() + "_" + language.getCountryCode() + "." + ext;
Path path = Paths.get(pathDownload + "/" + nameFile);
try {
-----------------------------------------------------------------------------
Files.write(path, file.getBytes());
------------>after this instruction, angular reloads the page<---------------
-----------------------------------------------------------------------------
} catch (IOException e) {
logger.error(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
return ResponseEntity.status(HttpStatus.OK).body(gson.toJson(path.toString()));
}
return ResponseEntity.status(HttpStatus.OK).body(null);
}
角组件
downloadFile(fileId: string) {
let link: string;
this.http.downloadFile(fileId).subscribe(
data => {
link = data;
}
);
console.log(link);
return link;
}
角服务
downloadFile(fileId: string) {
const myheader = new HttpHeaders().set('Authorization', this.token);
return this.http.get<string>(this.url.concat('files/downloads/' + fileId), { headers: myheader });
}
我只是希望在不重新加载角度组件的情况下从 spring boot api 获得响应。
(在邮递员中它必须如此工作)
解决方案
启动角度应用程序(而不是ng serve
)
ng serve --live-reload false
--liveReload=true|false
是否在更改时重新加载页面,使用实时重新加载。默认值: 真
备择方案
ng serve --no-live-reload
ng serve --live-reload=false
在package.json中创建命令为
"ng noreload" : "ng serve --live-reload=false"
推荐阅读
- laravel - 我的作曲家给我 mkdir() 权限被拒绝异常
- python - 如何导出已安装的 pip 包
- hibernate - SpringBoot 2.1.5.RELEASE 中的@OneToMany Spring Data JPA 未获取所有数据
- python - 如何在这个 DataFrame 上使用 Pandas 聚合函数?
- dynamics-crm - 如何使用 Power BI 公开 OData 源?
- azure - 如何在 Azure 服务总线主题订阅的消息中指定发件人订阅?
- jenkins - 自动将 SVN 文件夹内容提交到仓库
- html - 我想更改 widgetUserBox 的 css
- java - Selenium“单击”命令在身份验证完成之前不会完成
- javascript - 如何使用点击功能在同一个ajax调用成功时下载多个文件?