Aurelia 的视图模型 类 中是否有任何析构函数?

Is there any destructor in Aurelia's view-model classes?

Aurelia 的视图模型中是否有一些析构函数或 Dispose 方法 类?基本上我有以下代码:

import {apiFetchClient} from 'common/restClient/apiFetchClient';
import {json} from 'aurelia-fetch-client';
import {inject} from 'aurelia-framework';

@inject(apiFetchClient)
export class BetsList {

  constructor(apiFetchClient){
    var timer = setInterval(()=>this._fetchBets(), 1000);

    this.apiFetchClient = apiFetchClient;
    this.betSystems = [];
    this._fetchBets();
  }

  _fetchBets(){
    this.apiFetchClient
      .fetch('/bets')
      .then(response => response.json())
      .then(data => this.betSystems = data);
  }
}

我想在视图即将被销毁时终止计时器。

attacheddetached view lifecycle 挂钩添加到您的视图模型以选择在这些事件发生时收到通知。

  • created(view:View) - Invoked after both the view and view-model have been created. Allows your behavior to have direct access to the View instance.
  • bind(bindingContext:any) - Invoked when the databinding engine binds the view. The binding context is the instance that the view is databound to.
  • unbind() - Invoked when the databinding engine unbinds the view.
  • attached() - Invoked when the view that contains the extension is attached to the DOM.
  • detached() - Invoked when the view that contains the extension is detached from the DOM.

http://aurelia.io/docs.html#extending-html

import {apiFetchClient} from 'common/restClient/apiFetchClient';
import {json} from 'aurelia-fetch-client';
import {inject} from 'aurelia-framework';

@inject(apiFetchClient)
export class BetsList {

  constructor(apiFetchClient){
    this.apiFetchClient = apiFetchClient;
    this.betSystems = [];
    this._fetchBets();
  }

  _fetchBets(){
    this.apiFetchClient
      .fetch('/bets')
      .then(response => response.json())
      .then(data => this.betSystems = data);
  }

  attached() {
    this.interval = setInterval(() => this._fetchBets(), 1000);
  }

  detached() {
    clearInterval(this.interval);
  }
}