Skip to content

Commit 9c34b97

Browse files
Wykksjelbourn
authored andcommitted
fix(schematics): use ngAfterViewInit instead of ngOnInit in table (#15446)
According to the offical doc of Angular, view queries are set before the ngAfterViewInit callback is called. https://angular.io/api/core/ViewChild ngOnInit works with the current version of Angular, but it can change in the future.
1 parent 6ccf5a2 commit 9c34b97

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/lib/schematics/ng-generate/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit, ViewChild<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core';
1+
import { AfterViewInit, Component, ViewChild<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core';
22
import { MatPaginator, MatSort } from '@angular/material';
33
import { <%= classify(name) %>DataSource } from './<%= dasherize(name) %>-datasource';
44

@@ -15,15 +15,15 @@ import { <%= classify(name) %>DataSource } from './<%= dasherize(name) %>-dataso
1515
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
1616
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
1717
})
18-
export class <%= classify(name) %>Component implements OnInit {
18+
export class <%= classify(name) %>Component implements AfterViewInit {
1919
@ViewChild(MatPaginator) paginator: MatPaginator;
2020
@ViewChild(MatSort) sort: MatSort;
2121
dataSource: <%= classify(name) %>DataSource;
2222

2323
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
2424
displayedColumns = ['id', 'name'];
2525

26-
ngOnInit() {
26+
ngAfterViewInit() {
2727
this.dataSource = new <%= classify(name) %>DataSource(this.paginator, this.sort);
2828
}
2929
}

0 commit comments

Comments
 (0)