Vue: 计算属性未执行的场景

Created on 31 Mar 2016  ·  1Comment  ·  Source: vuejs/vue

代码示例:https://jsfiddle.net/3vj8ojLa/

粗浅的理解,当a=0时,由于v-if=“a”会把包含计算属性b的节点剔除。理论上没有计算的必要性了。

但这样就导致this.tip=‘changed’这条语句不能被执行。出现了盲区。

回头想想,这个场景用$watch视乎更合适。

求指教。

Most helpful comment

    b: function() {
      this.tip='changed';
      return this.a + 1;
    }

computed相当于getter函数,当然是只有get的时候才会执行对tip的赋值。
所以这并不是一个bug。

这种场景我能想到的也就是在请求数据的时候挂上钩子,统计请求的发生的次数了。
在getter里面执行赋值是违反单一职责原则的操作,应该尽量避免。

>All comments

    b: function() {
      this.tip='changed';
      return this.a + 1;
    }

computed相当于getter函数,当然是只有get的时候才会执行对tip的赋值。
所以这并不是一个bug。

这种场景我能想到的也就是在请求数据的时候挂上钩子,统计请求的发生的次数了。
在getter里面执行赋值是违反单一职责原则的操作,应该尽量避免。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paceband picture paceband  ·  3Comments

Jokcy picture Jokcy  ·  3Comments

bfis picture bfis  ·  3Comments

robertleeplummerjr picture robertleeplummerjr  ·  3Comments

hiendv picture hiendv  ·  3Comments