0

I am following the guidance However, the @ViewChild and ngAfterViewInit do not work for me. The error information is like this. It said I miss one argument. And I have tried to add 2 arguments like this, but it still does not work. And the ngAfterViewInit also has some problems.screenshot

Thanks a lot!!!!!

Community
  • 1
  • 1
hyzheng
  • 11
  • 4
  • please share your code i stacblitz, we cannot help you without seeing the full code – Akhil Aravind Jun 11 '19 at 09:36
  • my code is almost as same as the [link](https://stackblitz.com/angular/dnbermjydavk?file=app%2Ftable-overview-example.html) but i use and instead of . i dont know if it will influence the result> – hyzheng Jun 11 '19 at 09:40
  • if its same, it should work like a charm. create your own stackblitz and please share the link. we will help – Akhil Aravind Jun 11 '19 at 09:41

1 Answers1

1

Note: If you use Angular 8

In angular 8 @ViewChild is require two argument. before 8 version @ViewChild require only one argument. Please see below code.

Example: @ViewChild

@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;

you must implements AfterViewInit before use ngAfterViewInit

Example: ngAfterViewInit

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
})
export class ListComponent implements AfterViewInit { // <-- implements here

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

    constructor(){}

    ngAfterViewInit() {

    }

}
Bharat Chauhan
  • 3,204
  • 5
  • 39
  • 52
  • thanks for your answer !! The @ViewChild works for me now after I declare it according your method. However, the ngAfterViewInit() still has problem. If add this.dataSource.paginator = this.paginator ; this.dataSource.sort = this.sort; in constructor(){}, it works well. But if I put them in ngAfterViewInit(), it doesn't work! do you know why? Many thanks! – hyzheng Jun 11 '19 at 10:04