角度:防止资产文件夹发生更改时自动重新编译

时间:2019-09-13 07:44:48

标签: html angular spring-boot livereload

我正在尝试从Spring Boot生成的文件下载到angular项目的资产中。当我从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;
  }

Angular服务

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获得响应,而无需重新加载角度组件。

(在邮递员中,它必须按要求工作)

2 个答案:

答案 0 :(得分:1)

以(而不是ng serve的身份启动角度应用程序

ng serve --live-reload false
  

--liveReload=true|false是否在更改时使用实时重新加载页面。

     

默认值:

Angular Commands

替代品

  • ng serve --no-live-reload

  • ng serve --live-reload=false

  • package.json 中以"ng noreload" : "ng serve --live-reload=false"

  • 创建命令

答案 1 :(得分:0)

解决方案 根本不起作用的原因有两个:

  • 将功能绑定到html组件模板中会产生无限循环,这是一个不好的实践
  • 在angular的静态文件夹中的资产中创建文件,因此每次使用新文件更新该文件夹或在angular.json中链接到资产的外部文件夹时,都会重新加载组件
  • 所以我修改了我的下载功能,该功能通过响应返回文件的所有文本,然后创建了一个Blob文件(https://stackblitz.com/edit/angular-blob-file-download?file=app%2Fapp.component.ts),当用户单击下载按钮时,将在该文件中进行下载