2017-09-27 12 views
1

オンラインの例に基づいて、Vue.jsでディレクティブを配線しようとする際に問題が発生しています。ここで ローカルディレクティブVuejsの引数が定義されていません

<div id="hook-arguments-example" v-demo:foo.a.b="message"></div> 

それは、バインドまたは更新フック、 elbindingを使用している vnodeがすべて定義されていないかどうか、私はこれを実行するメインのVueのApp

const v = new Vue({ 
     el: '#app', 
     data: { 
      message: 'hello!' 
     }, 
     directives: { 
      demo: { 
       update: function (el, binding, vnode) { 
        console.log(el); 
        console.log(binding); 
        console.log(vnode); 
        var s = JSON.stringify 
        el.innerHTML = 
         'name: ' + s(binding.name) + '<br>' + 
         'value: ' + s(binding.value) + '<br>' + 
         'expression: ' + s(binding.expression) + '<br>' + 
         'argument: ' + s(binding.arg) + '<br>' + 
         'modifiers: ' + s(binding.modifiers) + '<br>' + 
         'vnode keys: ' + Object.keys(vnode).join(', ') 
       } 
      } 
     } 
    }) 

とディレクティブインラインのですか?私は間違って何をしていますか?

+0

私はそのペンからのコード、そうでないことをコピーする場合、これは、奇妙である – Bert

+0

https://codepen.io/Kradek/pen/zEwOvd?editors=1010正常に動作するようです作業。 divが表示される必要がありますか?私がvを持っているかのように、それが直前にそれを示すなら、私は指令を呼ぶ前にそれを壊すのでしょうか? –

+0

どのバージョンのVueを使用していますか?可視性は問題ではありません(ペンはそれを切り替える機能によって更新されます)。 – Bert

答えて

2

質問コードはVue 2で正しく機能します。OPは意図せずVue 1を使用していました。

console.clear() 
 

 
function onLifecycle(el, binding, vnode) { 
 
    var s = JSON.stringify 
 
    el.innerHTML = 
 
    'name: ' + s(binding.name) + '<br>' + 
 
    'value: ' + s(binding.value) + '<br>' + 
 
    'expression: ' + s(binding.expression) + '<br>' + 
 
    'argument: ' + s(binding.arg) + '<br>' + 
 
    'modifiers: ' + s(binding.modifiers) + '<br>' + 
 
    'vnode keys: ' + Object.keys(vnode).join(', ') 
 
} 
 

 
const v = new Vue({ 
 
    el: '#app', 
 
    data: { 
 
    message: 'hello!', 
 
    visible: false 
 
    }, 
 
    directives: { 
 
    demo: { 
 
     bind: onLifecycle , 
 
     update: onLifecycle 
 
    } 
 
    } 
 
})
<script src="https://unpkg.com/[email protected]"></script> 
 
<div id="app"> 
 
    <div v-if="visible"> 
 
    <div id="hook-arguments-example" v-demo:foo.a.b="message"></div> 
 
    </div> 
 
    <button @click="message='world'">Update Message</button> 
 
    <button @click="visible=!visible">Toggle Visible</button> 
 
</div>

関連する問題