this.test
переменная undefined
внутри ngAfterViewInit
, но может получить желаемый результат на ngOnInit
. переменная должна быть глобальной для обоих жизненных циклов. Я также пробовал использовать NgZone
и ChangeDetectorRef
, он все тот же.
import { Component, VERSION } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
test: string;
async get() {
try {
return 'new';
} catch (error) {
return '';
}
}
async ngOnInit() {
this.test = await this.get();
console.log(this.test);
}
async ngAfterViewInit() {
console.log(this.test); // <- returning undefined
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>