Vue: 自定义指令能否动态侦听内联语句(acceptStatement)里的对象变化?( Can custom directive detect changing in inline statement ?? )

Created on 15 Mar 2016  ·  4Comments  ·  Source: vuejs/vue

Vue.version "1.0.17"

{
    template:`
    <div 
          v-for="item in items"  
          v-custom-directive='!!item.active' 
          v-on:click='click(item)'>
                {{ item.name }}
    </div>`

    click(item){
        item.active = !item.active;// 这里应该触发v-custom-directive才对吧?
        //item.active do changed but 'v-custom-directive' not tigger update
    }
}
Vue.directive("custom-directive",{
    acceptStatement:true,
    // ,deep:true // i tried deep too but seems noting happen as well
    ,update (fn) {
           console.log(!!fn()) // 只触发了一次(only tigger once) 
           //expected tigger on "item.active" update
    }
})

Most helpful comment

acceptStatement 的作用是当你写比如 v-on="a++",传递给 v-on 的值不是对 a++ 求值的结果,而是一个会执行 a++ 的函数。

All 4 comments

acceptStatement 使得指令返回的是一个函数

function () {
  return !!item.active
}

函数没有被调用自然不会追踪依赖。
问题是你这里根本不需要用 acceptStatement 啊。

reply: @yyx990803

console.log(!!fn()) (倒数第三行) 的时候不是调用了一次?

哦哦明白了,我以为!!xxx也是内联语句,原来内联语句是指自增自减运算?

acceptStatement 的作用是当你写比如 v-on="a++",传递给 v-on 的值不是对 a++ 求值的结果,而是一个会执行 a++ 的函数。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aviggngyv picture aviggngyv  ·  3Comments

6pm picture 6pm  ·  3Comments

wufeng87 picture wufeng87  ·  3Comments

fergaldoyle picture fergaldoyle  ·  3Comments

bdedardel picture bdedardel  ·  3Comments