1
私がここで達成しようとしているのは、Item-card-product-choices.vue
の項目をクリックするとselectPC
と引き数productChoice.id
がトリガーされ、このイベントと同時に引数を取得します。私はthe docに従うが、親はselectPC
を聞いていないようだ。ですから、次の質問を解決してください。Vue2 Laravel5.3イベントで子供から親にデータを送信する方法
- 子供のイベントを聞くために私の方法が間違っていますか?
- 親が聞くと、子どもがそのイベントで使用する議論をどのように得るか?おかげさまで
項目-カード製品choices.vue
<template>
<ul class="product-choices">
<li
v-for="productChoice in productChoices"
class="product-choice"
@click.prevent="selectPC(productChoice.id)"
>
<svg v-if="productChoice.color === 'red'" width="20" height="20">
<rect width="20" height="20" style="fill:#FF3333"></rect>
</svg>
......
......
......
</li>
</ul>
</template>
<script>
export default {
props:[
'index',
'product'
],
data() {
return {
productChoices:{},
}
},
methods:{
selectPC(productChoice){
var vm = this;
vm.$emit('select',productChoice)
},
}
}
</script>
Parent.vue
<template>
<div>
....
<product-choices :product="product" @selectPC="getSelected(productChoice)"></product-choices>
....
</div>
</template>
<script>
import productChoices from './Item-card-product-choices.vue';
export default {
components:{
'product-choices':productChoices
},
data(){
return{
productChoiceSelected:{},
}
},
methods:{
getSelected(selected){
var vm = this
alert(1) // this is what I added to test if it listens
vm.$on('select',function(selected){
vm.$http.get('/getProductChoice/'+vm.product.id+'/'+selected).then((response)=>{
vm.productChoiceSelected = response.data
});
})
}
}
</script>
をどちらか
$on
リスナーを追加...べきではありませんか私のコードでやっていることではありませんか? – warmjaijai@warmjaijai子コンポーネントはまだイベントを '$ emit'する必要があります。そしてあなたが共有した文書で言われているように... [* "子どもが放出するイベントを聴くために' $ on 'を使うことはできません。テンプレート "*"(https:// vuejs.org/v2/guide/components.html#Using-v-on-with-Custom-Events)。 「@select = "..." 'は、' v-onと同じです:select = "..." ' – Phil
ああ、私はコード内でemitを見たのですが、docではメソッドのインクリメントとemitインクリメント混乱した。 @Phil – warmjaijai