在处理剩余的DOM之前,在Vue中切换活动状态

时间:2016-07-28 13:16:01

标签: javascript performance dom vue.js

我目前有一个项目,我最近搬到了Vue.js.我已经完成了迁移,一切正常,我唯一想要改进的是,平板电脑等设备上的webapps响应时间没有足够的处理能力来在相当长的时间内计算所有的变化。

目前我有一个项目列表,当您选择一项时,它会更新右侧的DOM以及所有计算的数据,并使用v-bind:class属性设置活动类。

当Vue计算并在右边显示更新的DOM信息时,有没有办法在/异步之前切换新的活动类并删除前一个(最终转换)?

1 个答案:

答案 0 :(得分:1)

你必须将昂贵的函数包装在一个承诺中,或者至少是超时。

someMethod() {
  var _this = this
  this.setnewClassHere

  var mypromise = new Promise(function(resolve, reject) {
    _this.expensiveFunction() // will runy async, so the above setNewClassHere will have effect before expensiveFunction is finished.

    resolve() // resolve the promise. This will execute the follwing .then() function.

  })
  .then(function(result) {
    _this.setBackTheClassHere
    // when async operation is finished, we set back the class
    // no idea if you need this in your case.
  })
}