Complete Vue.js 2 Web Development_ Practical guide to building end-to-end web development solutions with Vue.js 2

(singke) #1
Using Vue-Router Dynamic Routes to Load Data Chapter 9

Attach a click event to the Add to basket button and create a new method on the


component. This method will require the variation object to be passed in, so the correct


one could be added to the basket. For now, add a JavaScript alert() to confirm you have


the right one:


const ProductPage = {
name: 'ProductPage',

template: `<div>
<div v-if="product">
<div class="images" v-if="image">
<div class="main">
<img
:src="image.source"
:alt="image.alt || product.title">
</div>

<div class="thumbnails" v-if="product.images.length > 1">
<template v-for="img in product.images">
<img
:src="img.source"
:alt="img.alt || product.title"
width="100"
@click="updateImage(img)">
</template>
</div>
</div>

<h1>{{ product.title }}</h1>

<div class="meta">
<span>
Manufacturer: <strong>{{ product.vendor.title }}</strong>
</span>
<span v-if="product.type">
Category: <strong>{{ product.type }}</strong>
</span>
</div>

<div class="variations">
<table>
<tr v-for="variation in product.variationProducts"
:key="variation.barcode">
<td v-html="variantTitle(variation)"></td>
<td>{{ variation.quantity }}</td>
<td>\${{ variation.price }}</td>
<td><button @click="addToBasket(variation)">Add to
Free download pdf