1. 程式人生 > >Vue list迴圈時 點選當前元素顯示相應的詳細內容

Vue list迴圈時 點選當前元素顯示相應的詳細內容

列表迴圈

	<ul>
		<li v-for="(good,index) in goods" :key="good.id"class="food-list-hook">
			<div class="class-title">
				{{good.class}}
			</div>
			<div v-for="(good,indexs) in good.list" :key="good.id"  >
				<div class="item"  @click="selectGood($event,good)">
					<div class="item-self">
						<div class="title">
							{{good.name}}
						</div>
						<div class="price">
							<i class="fa fa-cny"></i>
							<span class="redtext">
								{{good.price}}
							</span>
						</div>
					</div>
				</div>
			</div>
		</li>
	</ul>

顯示的內容

<div class="menu" :food="selectedFood">
	<div class="menu-img">
		<img :src="selectedFood.img" alt=""/>
	</div>
	<div class="menu-con">
		<div class="title">
			{{selectedFood.name}}
		</div>
		<div class="price">
			<i class="fa fa-cny"></i>
			<span class="redtext">
				{{selectedFood.price}}
			</span>
		</div>
		<div class="destitle">
			{{selectedFood.int}}
		</div>
	</div>
</div>

點選獲取並放入資料

new Vue({
	el: '#app',
	data (){
	    return{
      	    goods: [],
	 	    selectedFood: {}    //設定顯示的陣列
    }
  },
	methods: {
    	selectFood($event,food){
			this.selectedFood = food;    //將當前點選的資料放入顯示陣列

			console.log(food)
      },
    }
})