1+ < html >
2+ < head >
3+ < script src ="https://unpkg.com/vue "> </ script >
4+ < link href ="./assets/style.css " rel ="stylesheet ">
5+ < link href ="https://use.fontawesome.com/releases/v5.8.1/css/all.css " rel ="stylesheet ">
6+ < title > Reviewable Socks</ title >
7+ </ head >
8+ < body >
9+ < div id ="app ">
10+ < div class ="nav-bar "> < h1 > Reviewable Socks</ h1 > </ div >
11+
12+ < product :premium ="premium " @add-to-cart ="updateCart "> </ product >
13+
14+ < div class ="cart ">
15+ {{ cart.length }}
16+ < i class ="fas fa-shopping-cart "> </ i >
17+ </ div >
18+ </ div >
19+
20+ < a href ="02-ComputedSocks.html " class ="prev ">
21+ < i class ="fas fa-arrow-left "> </ i > moving back
22+ </ a >
23+ </ body >
24+ </ html >
25+
26+
27+ < script type ="text/javascript ">
28+ const comp = Vue . component ( 'product' , {
29+ // Props with validation
30+ props : {
31+ premium : { type : Boolean , required : true , default : false }
32+ } ,
33+ // Without validation
34+ // props: ['premium'],
35+ template : `
36+ <div class="product">
37+ <div class="product-image">
38+ <img :src="image">
39+ </div>
40+
41+ <div class="product-info">
42+ <h1>{{ product }}</h1>
43+
44+ <div>
45+ <p v-if="inventory > 10">In Stock</p>
46+ <p v-else-if="inventory">Almost sold out</p>
47+ <p v-else>Out of Stock</p>
48+ </div>
49+
50+ <p v-if="premium">FREE Shipping</p>
51+ <p v-else>Shipping: $4.99</p>
52+
53+ <button
54+ v-on:click="addToCart"
55+ :disabled="!inventory"
56+ :class="inventory ? '' : 'disabledButton'"
57+ >
58+ Add to Cart
59+ </button>
60+
61+ <div v-for="(variant, index) in variants"
62+ :key="variant.id"
63+ class="color-box"
64+ :style="{backgroundColor: variant.color}"
65+ @mouseover="selectedVariantIndex = index">
66+ </div>
67+ </div>
68+ </div>
69+ ` ,
70+ data ( ) {
71+ // Return a fresh object reference each time
72+ return {
73+ product : 'Socks' ,
74+ selectedVariantIndex : 0 ,
75+ variants : [
76+ { id : 1 , color : 'green' } ,
77+ { id : 2 , color : 'blue' }
78+ ] ,
79+ inventory : 3 ,
80+ } ;
81+ } ,
82+ methods : {
83+ addToCart ( ) {
84+ this . inventory -- ;
85+ const selectedVariant = this . variants [ this . selectedVariantIndex ] ;
86+ this . $emit ( 'add-to-cart' , { product : this . product , variant : selectedVariant } ) ;
87+ }
88+ } ,
89+ computed : {
90+ image ( ) {
91+ const color = this . variants [ this . selectedVariantIndex ] . color ;
92+ return `./assets/socks-${ color } .jpg` ;
93+ }
94+ }
95+ } )
96+
97+ const app = new Vue ( {
98+ el : '#app' ,
99+ data : {
100+ premium : true ,
101+ cart : [ ]
102+ } ,
103+ methods : {
104+ updateCart ( product ) {
105+ console . log ( 'Adding to cart:' , product ) ;
106+ this . cart . push ( product ) ;
107+ }
108+ }
109+ } ) ;
110+ </ script >
111+
112+
113+ <!-- livereload -->
114+ < script > document . write ( '<script src="http://' + ( location . host || 'localhost' ) . split ( ':' ) [ 0 ] + ':35729/livereload.js?snipver=1"></' + 'script>' ) </ script >
0 commit comments