コンポーネントの子の子に小道具としてクリックメソッドを渡そうとしています。しかし、メソッドがパラメータを取らないとうまくいくようです。ただし、パラメータが必要な場合は、Vue.jsからInvalid handler for event "click": got undefined
というエラーが送信されます。ここでコンポーネントでVue.jsメソッドが未定義です
は私のトップレベルのコンポーネントです:
new Vue({
el: '#app',
created: function() {
},
methods: {
action1: function() {
console.log('--action 1--')
},
action2: function (word) {
console.log('--action 2--', word)
}
}
});
ここでは私の子コンポーネントです:
Vue.component('parent-component', {
template: '#parent-component',
data() {
return {
menuItems: [
{
label: 'Action 1',
onClick : this.action1
},
{
label: 'Action 2',
onClick : this.action2('two')
}
]
}
},
props: ['action1', 'action2']
});
<template id="parent-component">
<action-menu :actions="menuItems"></action-menu>
</template>
そして、ここでは私の最も低いレベルのコンポーネントである:ここで
Vue.component('action-menu', {
template: '#action-menu',
props: ['actions'],
});
<template id="action-menu">
<div>
<div v-for="action in actions">
<button @click="action.onClick">{{ action.label }}</button>
</div>
</div>
</template>
は私のバイオリンです - http://jsfiddle.net/11sfg1p8/ - 最初のボタンがどのように機能するかを確認しますが、2番目のボタンは表示されず、唯一の差分erenceは2番目のボタンでパラメータをとります。誰が何が起こっているか考えている?
ありがとうございます!
あなたは、このフィドル(またはあなたが意図しているもの)の中で 'this.action2.bind(null、 'two')'を意味することを確かめてください。 – Bert
@BertEvans申し訳ありません、そして 'this.action2.bind(this、 'two')'は良い選択かもしれませんか? –
'bind'の最初の引数は、' this'がバインドされた関数の中にあるものです。あなたは 'this'は' two'でなければならないと言っています。 – Bert