Javascript 加载时未定义值

Javascript 加载时未定义值,javascript,angular,typescript,angular-services,Javascript,Angular,Typescript,Angular Services,我正在开发angular应用程序 在我的服务文件中,我创建了一个函数configure。它在组件的AfterViewInit中被调用 但是在加载时,this.config是未定义的,如果我在setTimeOut中使用它,我可以访问this.config的值 下面的代码可以工作 configure() { setTimeout(() => { if(this.config) { this.apply(); } }, 200);

我正在开发angular应用程序

在我的服务文件中,我创建了一个函数
configure
。它在组件的
AfterViewInit
中被调用

但是在加载时,
this.config
是未定义的,如果我在
setTimeOut
中使用它,我可以访问
this.config

下面的代码可以工作

  configure() {
    setTimeout(() => {
      if(this.config) {
        this.apply();
      } 
    }, 200);
  }
有没有更好的办法?不使用setTimeOut

请帮助

尝试使用方法

将以下代码添加到@Component decorator

  changeDetection: ChangeDetectionStrategy.OnPush,

你能试试下面的代码吗

configure() {
    var self = this;
    setTimeout(() => {
      if(self.config) {
        self.apply();
      } 
    }, 200);
}

this.config
何时何地设置?我不确定,但是
this.config
以不同的功能加载到同一个服务文件中。由于它是一个大的应用程序,我无法跟上它
configure() {
    var self = this;
    setTimeout(() => {
      if(self.config) {
        self.apply();
      } 
    }, 200);
}