gpt4 book ai didi

javascript - Aurelia 的 View 模型类中是否有任何析构函数?

转载 作者:行者123 更新时间:2023-11-29 10:11:41 25 4
gpt4 key购买 nike

Aurelia 的 View 模型类中是否有一些析构函数或 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);
}
}

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

最佳答案

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

  • 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);
}
}

关于javascript - Aurelia 的 View 模型类中是否有任何析构函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32284862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com