2016-12-14 133 views
0

我有以下孩子的方法Vue公司2.0 Laravel 5.3數組和對象通過的forEach

methods:{ 
    selectedPC(selectedProductChoice){ 
    this.$parent.$children.forEach(choice=>{ 
     choice.isActive = (choice.id == selectedProductChoice.id) 
    }) 
    } 
} 

和我的孩子模板下面

<template> 
    <transition name="fade"> 
    <li 
    class="product-choice" 
    :class="{'selected': productChoice.isActive}" 
    @click.prevent="selectedPC(productChoice)" 
    > 
     ...... 
     ...... 
     ...... 
    </li> 
    </transition> 
</template> 

和這裏就是孩子被稱爲passsing父模板

<ul class="product-choices" v-show="thumbnailHover"> 
    <product-choices v-for="(productChoice, index) in productChoices" :key="productChoice.id" :product="product" :product-choice="productChoice" :index="index"></product-choices> 
</ul> 

不確定爲什麼,但選擇子模板中的forEach是一個對象,它不允許我讀取choice.idundefined,因爲我試圖以alert它)。它是數組對象的問題?我有點關注Vue 2 Laracast的標籤教程供您參考。

回答

0

你必須嘗試child.$data訪問與孩子相關的數據,如下列:

methods:{ 
    selectedPC(selectedProductChoice){ 
    this.$parent.$children.forEach(choice=>{ 
     choice.$data.isActive = (choice.$data.id == selectedProductChoice.id) 
    }) 
    } 
} 
+0

以及它是有道理的我,但它保持未定義警報(「選擇$ data.id」) – warmjaijai