@Viewchild无法看到matSort [英] @Viewchild can not see matSort

查看:238
本文介绍了@Viewchild无法看到matSort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Angular应用程序中,我的@ViewChild实例无法填充HTL matSort.

In my Angular application, my @ViewChild instance is failing to fill HTL matSort.

mycomponent.ts:

import { MatSort } from '@angular/material';

export class MyClassComponent {
     @ViewChild(MatSort) sort: MatSort;

}

ngOnInit() {
    console.log(this.sort); // undefined
}

mycomponent.html:

<mat-table #table [dataSource]="dataSource" matSort>
           .......................................................
           .......................................................

</mat-table>

我需要使用matSort中的sortChange,但始终返回未定义的值.

I need to use sortChange from matSort but it is always returned undefined.

推荐答案

它将被初始化并在 AfterViewInit 生命周期挂钩:

It will be initialized and available in the AfterViewInit lifecycle hook:

export class MyClassComponent implements AfterViewInit {
  @ViewChild(MatSort) sort: MatSort;

  ngAfterViewInit() {
    console.log(this.sort); // MatSort{}
  }
}

有关生命周期挂钩的更多信息,请参见: https://angular.io/guide/lifecycle-hooks .

More info on lifecycle hooks here: https://angular.io/guide/lifecycle-hooks.

这篇关于@Viewchild无法看到matSort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆