11const comp = Vue . component ( 'product' , {
22 // Props with validation
33 props : {
4- premium : { type : Boolean , required : true , default : false }
4+ premium : { type : Boolean , required : true , default : false } ,
5+ // Types: String, Number, Array, Object, Function, Promise, any ctor
6+ // Null and undefined always pass validation
7+ // oneOf: [String, Number],
8+ // special: {
9+ // default() { return 'calculatedValue'; },
10+ // validator(value) { return true; }
11+ // }
512 } ,
613 // Without validation
714 // props: ['premium'],
815 template : `
16+ <!-- Must have single root element -->
917 <div class="product">
1018 <div class="product-image">
1119 <img :src="image">
1220 </div>
1321 <div class="product-info">
14- <h1>{{ product }}</h1>
15- <p v-if="premium">FREE Shipping</p>
22+ <h1 v-text="product"></h1>
23+
24+ <p v-if="premium" v-html="'<b>FREE Shipping</b>'"></p>
1625 <p v-else>Shipping: $4.99</p>
17- <button v-on:click="addToCart" class="add-to-cart" :class="inventory ? '' : 'disabledButton'">
26+
27+ <button @click="addToCart" class="add-to-cart" :class="inventory ? '' : 'disabledButton'">
1828 Add to Cart
1929 </button>
30+
2031 <div v-for="(variant, index) in variants"
2132 :key="variant.id"
2233 class="color-box"
34+ :class="{active: selectedVariantIndex === index}"
2335 :style="{backgroundColor: variant.color}"
24- @mouseover="selectedVariantIndex = index">
25- </div>
36+ @mouseover="selectedVariantIndex = index"
37+ > </div>
2638 </div>
2739 </div>
2840 ` ,
2941 data ( ) {
30- // Return a fresh object reference each time
42+ // data is a function inside a Vue.component()
43+ // Do return a new object reference each time.
3144 return {
3245 product : 'Socks' ,
3346 selectedVariantIndex : 0 ,
@@ -49,13 +62,29 @@ const comp = Vue.component('product', {
4962 image ( ) {
5063 const color = this . variants [ this . selectedVariantIndex ] . color ;
5164 return `./assets/socks-${ color } .jpg` ;
52- }
65+ } ,
66+ // If you also need a setter:
67+ // title: {
68+ // get() {
69+ // return this.product;
70+ // },
71+ // set(newValue) {
72+ // this.product = newValue;
73+ // }
74+ // }
5375 }
5476} ) ;
5577
5678
5779
58- const app = new Vue ( {
80+
81+
82+
83+
84+
85+
86+
87+ const vm = new Vue ( {
5988 el : '#app' ,
6089 data : {
6190 premium : true ,
0 commit comments