ObjectUnsubscribedError

ObjectUnsubscribedError

切换“点击”按钮(view_click)时出现ObjectUnsubscribedError。

ObjectUnsubscribedError {{name:“ObjectUnsubscribedError”,堆栈:
“ObjectUnsubscribedError:对象已取消订阅↵
一个…(ng:///AppModule/UserComponent.ngfactory.js:43:5)“,消息:“对象
退订”,ngDebugContext:DebugContext_,ngErrorLogger:ƒ}

查看如下:

 <button class="btn" (click)="view_click(e)">Click</button>
            <div class="container-fluid" *ngIf="list_v; ">
                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="header">
                                <h4 class="title">User List</h4>
                                <p class="category">List all users</p>
                            </div>
                            <div class="content table-responsive table-full-width">
                              <table datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="row-border hover" (click)="table_click($event.target)"></table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

组件如下:
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { DataTableDirective } from 'angular-datatables';
import { HttpClient } from '@angular/common/http';
import { Subject } from 'rxjs/Subject';

@Component({
  selector: 'app-user',
  templateUrl: 'user.component.html'
})
export class UserComponent implements OnInit, AfterViewInit {
  list_v=true;

  constructor(private http: HttpClient){ }

   view_click(e:HTMLElement){
       if(this.list_v){
           this.list_v=false;
       }else{
           this.list_v=true;
       }
   }

  @ViewChild(DataTableDirective)
  dtElement: DataTableDirective;

  dtOptions: DataTables.Settings = {};

  dtTrigger: Subject<any> = new Subject();

  ngOnInit(): void {
    this.dtOptions = {
      ajax: 'http://localhost/angular/backend/users',
      columns: [{
        title: 'ID',
        data: 'id'
      }, {
        title: 'Name',
        data: 'name'
      }, {
        title: 'Action',
        data: 'action'
      }]
    };
  }
  ngOnDestroy() {

}

  ngAfterViewInit(): void {
    this.dtTrigger.next();
  }

   table_click(e:HTMLElement){
    //   if(e.attributes.getNamedItem('delete_id')){
    //          var id=e.attributes.getNamedItem('delete_id').nodeValue;
    //      this.http.get('http://localhost/angular/backend/delete/user/'+id,)
      //     .subscribe(
      //    (data) => this.delete(data), //For Success Response
      //   err => {console.error(err)} //For Error Response
      // );
      // }

   }
   delete(data){
           this.rerender();
   }

  rerender(): void {
    this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
      // Destroy the table first
      dtInstance.destroy();
      // Call the dtTrigger to rerender again
      this.dtTrigger.next();
    });
  }
}

最佳答案

似乎在进行重新渲染时,它会删除所有侦听器,因为重新渲染不是按角度进行的。因此,在重新渲染后,单击侦听器以某种方式工作,但“table_click(e:HTMLElement)”却无法工作。因此,您需要检查“角度数据表”存储库中是否存在类似问题。
我不确定100%,但是事实确实如此。

10-04 22:51