你的
@ViewChild
如果尝试在上访问它,则未定义
ngOnInit
. 试试这个:
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatSlideToggle } from '@angular/material/slide-toggle';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.scss']
})
export class TestComponent implements AfterViewInit {
@ViewChild('testSlider', { static: false }) t: MatSlideToggle;
constructor(private changeDetectorRef:ChangeDetectorRef) { }
ngAfterViewInit() {
this.t.checked = true;
this.changeDetectorRef.detectChanges();
}
}