api服务变得不可观察

时间:2019-02-07 15:35:29

标签: angular typescript ionic-framework ionic3 ionic4

下面是我的{6} ionic 4应用程序的XmlDocument。我有一个观察站,它每2分钟触发一次api服务调用,以检查是否有新通知。第一个呼叫成功触发,之后,我收到一条错误消息,说app.component.js指向我的行can't find listModel of undefined。为什么这变得不确定?我该怎么做才能绕过此问题?

await this.api.listModel('Notification'

这是api服务调用

...
import { LoadingService } from './loading.service';
import { apiService } from './...-api.service';
...

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {
  public appPages = [
    {
      title: 'Home',
      url: '/notifications',
      icon: 'notifications'
    },
  ...
  public title = 'Places';
  public addrKeys: string[];
  public addr: object;
  public hasUnactionedNotification: any = false;
  public notificationObservable: any;
  public account: any;
  public initials: any;

  constructor(
    private platform: Platform,
    private api: apiService,
    ...
  ) {
    this.initializeApp();
    this.initializeNotifAndInitials();
  }

  ...

  async initializeNotifAndInitials() {
    this.refreshNotifications();
    this.account = await this.accountService.getAccount();
    this.initials = this.account.firstName != null ? this.account.firstName.charAt(0) + this.account.lastName.charAt(0) : '';
    this.notificationObservable = interval(0.5 * 60 * 1000);
    this.notificationObservable.subscribe(this.refreshNotifications);
  }

  async refreshNotifications() {
    let notifications = await this.api.listModel('Notification', null, null, {
      page: 0,
      size: 15
    });
    this.hasUnactionedNotification = !!_.filter(notifications, {
      isActioned: false
    }).length;
    //boot out users who are no longer logged in
    this.account = await this.accountService.getAccount();
    if (!this.account) {
       this.router.navigateByUrl('/login');
    }
  }

}

error

1 个答案:

答案 0 :(得分:1)

我认为问题在于您需要.bind(this)

即:

this.notificationObservable.subscribe(this.refreshNotifications.bind(this));

但是我个人不喜欢这种语法,我宁愿使用

this.notificationObservable.subscribe(() => this.refreshNotifications());