html - Angular:当 Assets 文件夹发生变化时防止自动重新编译

标签 html angular spring-boot livereload

我正在尝试在 angular 项目的 Assets 中下载从 spring boot 生成的文件。当我从 angular 服务调用 spring API 时,angular CLI 在 assets angular 文件夹中创建文件后重新编译项目,然后在从 spring boot API 获得响应之前重新加载页面。

我尝试通过多种方式从 angular 调用 spring boot API:

  • 在我的组件的 ngOnInit() 中调用 api
  • 在我的组件的构造函数中调用 api
  • 在单独的函数中调用 api
  • 在下载函数中使用 async await

  • 我不知道如何继续

    Spring Boot
     @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);
        }
    

    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 boot api 获得响应。

    (在 postman 中,它必须按原样工作)

    最佳答案

    启动 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:当 Assets 文件夹发生变化时防止自动重新编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57919343/

    相关文章:

    javascript - firebase angularfire2 typeahead 查询不起作用

    Angular DI 提供者与 viewProviders

    spring-boot - 在 Spring Boot 中 Autowiring 参数化构造函数

    PHP 简单 HTML DOM 解析

    html - 清理 angularjs Controller 和功能 - 用于具有多选定制的下拉列表

    html - 在 bootstrap 3 的 popover 中放置一个表单?

    spring - 不同端点的多个用户详细信息服务

    javascript - 各种样式值的 CSS 转换

    javascript - Angular 5 中带有 "Compare Password"验证的响应式(Reactive)表单

    spring-boot - Spring Boot 2.0.0.M4和Hibernate 5.2.11.Final找不到EntityManagerFactoryBuilder类型的bean