Angular: 防止资产文件夹发生变化时自动重新编译

Angular: prevent auto recompile when there is a change in assets folder

我正在尝试在 angular 项目的资产中下载从 spring 引导生成的文件。当我从 angular 服务调用 spring API 时,angular CLI 在资产 angular 文件夹中创建文件后重新编译项目,然后重新加载从 spring boot API.

获得响应之前的页面

我尝试以多种方式从 angular 调用 spring 启动 API:

我不知道如何继续

spring开机

 @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("[=11=]");

            /*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);
    }

Angular 组件

 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 });
  }

我只是希望在不重新加载 angular 组件的情况下从 spring 启动 api 获得响应。

(在邮递员中它必须按原样工作)

启动 angular 应用程序(而不是 ng serve

ng serve --live-reload false

--liveReload=true|false Whether to reload the page on change, using live-reload.

Default: true

Angular Commands

备选方案

  • ng serve --no-live-reload

  • ng serve --live-reload=false

  • package.json中创建命令为"ng noreload" : "ng serve --live-reload=false"

这根本不起作用有两个原因:

  • 在 HTML 组件模板中绑定函数会导致无限循环,这是一种不好的做法。
  • 在资产中创建文件,其中 Angular 是静态文件夹,因此每次该文件夹或 angular.json 资产中链接的外部文件夹更新为新文件时,它会重新加载组件

我修改了我的下载功能,通过响应 returns 文件的所有文本,然后我创建了一个 blob 文件 (https://stackblitz.com/edit/angular-blob-file-download?file=app%2Fapp.component.ts),当用户点击下载时,它将被下载按钮。