0

The problem is when I want to change tab, then it is show an error message: enter image description here

My code look like this:

 <mat-tab-group mat-align-tabs="start">
      <mat-tab label="tab1">
        <ng-template matTabContent>
          <app-list-table [logic]="this.logic" [groupable]="false"></app-list-table>
        </ng-template>
      </mat-tab>
      <mat-tab label="tab2">
        <ng-template matTabContent>
          <app-list-table [logic]="this.logic" [groupable]="true"></app-list-table>
        </ng-template>
      </mat-tab>
      <mat-tab label="tab3">
        <ng-template matTabContent>
          <app-group-table [logic]="this.logic"></app-group-table>
        </ng-template>
      </mat-tab>
    </mat-tab-group>

On tab change the app-list-table component.ts code:

export class ListTableComponent extends DestroyableBase implements OnInit, AfterViewInit {
 
  displayedColumns: string[] = [
    'open',
    'id',
    'dtCreated',
    'nmUser',
  ];
  dataSource = new MatTableDataSource<any>();
  

  @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  @ViewChild(MatSort, { static: true }) sort: MatSort;

  @Input() logic: ListService;
  @Input() groupable: boolean;

  constructor(private router: Router) {
    super();
  }

  ngAfterViewInit() {
    this.dataSource.sort = this.sort;
    this.dataSource.paginator = this.paginator;
   }
    
  ngOnInit() {
    this.logic.List$.pipe(takeUntil(this.destroy$)).subscribe((list) => { 
      this.dataSource.data = list;
    });
  }
}

The logic get data for datasource onInit, so when I switch tab it will be loaded. But I'm not sure, this cause the problem.

allpacka
  • 27
  • 1
  • 4
  • Why don't you define `logic` in `app-list-table` constructor? – N.F. Mar 29 '21 at 07:30
  • I use it at multiple components, thats why – allpacka Mar 29 '21 at 07:41
  • Service is singleton. Even if you define the instance of `ListService` multiple time, they refer to the same instance. I suggest you to define `logic` in `app-list-table` and not to pass as `@Input`. – N.F. Mar 29 '21 at 08:07

0 Answers0