title: Mat-sort is not working in angular mat-table
body: <p>My mat-table is working fine, but when adding mat-sort following the official api documentation, it fails at the ngAfterViewInit with the following message</p>
  
  <p><strong>Cannot set property 'sort' of undefined
   aq LeadsComponent.push../src/app/leads/leads.component.ts.LeadsComponent.ngAfterViewInit</strong></p>
  
  <p>There is already a SO post on Mat-table Sorting Demo and tried them but I still am not able to get it working.</p>
  
  <p>can some one hemp me solve this issue? The official example works with a "static" MatTableDataSourcedefined in the component itself, I am querying from my back-end, however.</p>
  
  <p>MatSortModule is already imported in app.module.ts, mat-sort-header directives are applied to the columns and the ngAfterViewInit is already exactly like in the official example...</p>
  
  <pre><code> export class LeadsComponent implements OnInit,AfterViewInit  {
  
    displayedColumns: string[] = ['name', 'leadStatus', 'mobile', 'email', 'actions'];
    dataSource: MatTableDataSource&lt;LeadsChildObject&gt;;
    @ViewChild(MatPaginator) paginator: MatPaginator;
    @ViewChild(MatSort) sort: MatSort;
  
    constructor(public dialog: MatDialog, private dashBoardService: LeadsService,
      private toast: ToastrService) {
    }
  
      ngAfterViewInit() {
          this.dataSource.sort = this.sort;
        }
  
        ngOnInit() {
          this.dashboardService.getFeedback.subscribe(
            res =&gt; {
              this.leadlist= res;
              this.dataSource = new MatTableDataSource(this.leadlist);
            }
          );
        }
      }
  }
  </code></pre>