Javascript 按钮单击事件未按角度重新发送

Javascript 按钮单击事件未按角度重新发送,javascript,angular,typescript,Javascript,Angular,Typescript,在我的.html文件中,我有以下代码:- 此处显示“数据导入”按钮 <button mat-menu-item (click)="download()"> <mat-icon>cloud_download</mat-icon> <span>Data Import</span> </button> 在downloadservice.ts文件中:- 这里创建的服务

在我的.html文件中,我有以下代码:- 此处显示“数据导入”按钮

<button mat-menu-item (click)="download()">
                <mat-icon>cloud_download</mat-icon>
                <span>Data Import</span>
</button>
在downloadservice.ts文件中:-

这里创建的服务将在后端调用api/下载

 export class DownloadService {
     etext : String;
    baseUrl: string = environment.apiUrl + '/Download';
      constructor(private http: HttpClient) { }

      getDownload() {
        return this.http.get(this.baseUrl);
        this.etext="The operation has been done";
      }
      }

当我点击数据导入按钮时,不会发生任何事情,也不会生成任何事件。

1-第二行不会执行,因为第一条语句有一个return关键字:

return this.http.get(this.baseUrl);
this.etext="The operation has been done";
2-正如MartinČuka在下面评论的那样,您需要订阅httpclient返回的可观测数据

this.downloadService.getDownload().subscribe(resp => { // do whatever });

什么都没有发生,因为httpClient正在返回Observable,您需要订阅它。 将subscribe添加到您的服务中

this.downloadService.getDownload().subscribe();
至于线路

this.etext="The operation has been done";

编译器会告诉您它是不可访问的,但是真正的问题是缺少订阅

我认为http请求被触发了。 但是,您不知道它何时完成,因为您没有订阅http.get返回的可观察对象

组件技术

订阅时要小心,订阅完成后必须取消订阅。

但这不是唯一的问题。什么都没有发生,因为httpClient正在返回Observable,您需要订阅它……这不是真的。您不必取消订阅httpClient。我会帮你的。您必须取消订阅自定义订阅。。。见文件。同样,没有http请求被触发。参见文档关于httpClient@MartinČuka,请参见下面关于此主题的讨论:正如他们在那里所说的,尽管Angular会自动取消订阅http请求,但您应该注意用户在收到http响应之前关闭选项卡的情况,这可能会导致性能问题。
this.etext="The operation has been done";
export class Component {

  constructor(private downloadService: DownloadService){}

    download(){
      this.downloadService.getDownload().subscribe(
        () => {
          // success code
        },
        (error) => {
         // error code
        }
      );
    }
}
 export class Component {
  constructor(private downloadService: DownloadService){}
    download(){
      this.downloadService.getDownload().subscribe(
        () => {
          // success code
        },
        (error) => {
         // error code
        }
      );
    }
}}