【问题标题】:Button click event is not reponding in AngularAngular 中的按钮单击事件没有响应
【发布时间】:2020-02-16 05:14:33
【问题描述】:

在我的 .html 文件中,我有以下代码:- 这里出现了数据导入按钮......

<button mat-menu-item (click)="download()">
                <mat-icon>cloud_download</mat-icon>
                <span>Data Import</span>
</button>

在 component.ts 文件中:-
这里我定义了点击按钮后要调用的函数::

  constructor(
           private downloadService: DownloadService
      )

    download(){
      this.downloadService.getDownload();

      }

在 downloadservice.ts 文件中:-

这里已经创建了服务,它将在后端调用 api /Download。

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

当我点击数据导入按钮时..没有任何反应,也没有生成任何事件。

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

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

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

    2- 正如 Martin Čuka 在下面评论的那样,您需要 subscribe httpclient 返回的 Observable。

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

    【讨论】:

    • 但这不是唯一的问题。什么都没有发生,因为 httpClient 正在返回 Observable 你需要 subscribe 到它...
    【解决方案2】:

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

    this.downloadService.getDownload().subscribe();
    

    至于线

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

    编译器会告诉你它无法访问,但真正的问题在于缺少 subscribe

    【讨论】:

      【解决方案3】:
       export class Component {
        constructor(private downloadService: DownloadService){}
          download(){
            this.downloadService.getDownload().subscribe(
              () => {
                // success code
              },
              (error) => {
               // error code
              }
            );
          }
      }}
      

      【讨论】:

        【解决方案4】:

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

        component.ts

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

        订阅时要小心,订阅完成后您必须取消订阅。 https://blog.angularindepth.com/the-best-way-to-unsubscribe-rxjs-observable-in-the-angular-applications-d8f9aa42f6a0

        【讨论】:

        • 这不是真的。您不必取消订阅 httpClient。 Angular 会为你做这件事。您必须取消订阅自定义订阅...请参阅文档 angular.io/guide/http。再一次,没有触发 http 请求。请参阅有关 httpClient 的文档
        • @MartinČuka,请参阅以下关于此主题的讨论:stackoverflow.com/a/57274287/12171299 stackoverflow.com/a/51850733/12171299 正如他们所说,虽然 Angular 会自动取消订阅 http 请求,但您应该注意用户关闭收到 http 响应之前的选项卡,这可能会导致性能问题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多