【发布时间】:2020-09-18 17:29:44
【问题描述】:
声明变量
export class TestComponent implements OnInit {
testVar:string;
然后初始化
ngOnInit() {
this.somefunction works.subscribe(
this.testVar='testData';
console.log('log1 '+this.testVar);
);
console.log('log2 '+this.testVar);
}
现在触发 AfterViewInit:
ngAfterViewInit() {
console.log('log3 '+this.testVar);
结果是
log1 testData
log2 undefined
log3 undefined
问题是为什么 log2 和 log 3 给出未定义的 testVar,我该如何解决?
【问题讨论】:
标签: angular variables initialization ngoninit