Last active
March 31, 2019 11:02
-
-
Save eladcandroid/d5af5f781c46a6fdcffc249dd955277f to your computer and use it in GitHub Desktop.
GoCode Vue.js ItemsList example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<title>Page Title</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
<script src="lib/vue.js"></script> | |
</head> | |
<body> | |
<style> | |
.cart-badge { | |
background-color: #6394f8; | |
border-radius: 10px; | |
color: white; | |
display: inline-block; | |
font-size: 18px; | |
line-height: 1; | |
padding: 3px 7px; | |
text-align: center; | |
vertical-align: middle; | |
white-space: nowrap; | |
position: relative; | |
bottom: 30px; | |
} | |
.item { | |
border: 1px solid black; | |
height: 300px; | |
float: left; | |
padding: 10px; | |
margin: 10px; | |
text-align: center; | |
} | |
.add-to-cart { | |
text-align: center; | |
padding: 10px; | |
} | |
.add-to-cart button { | |
background: #6394f8; | |
color: #FFF; | |
font-size: 16px; | |
border-radius: 10px; | |
padding: 10px; | |
} | |
</style> | |
<div id="app"></div> | |
<script> | |
Vue.component('item', { | |
props: ['description', 'img'], | |
template: `<div class="item"> | |
<div class="add-to-cart"><button @click="$emit('add-to-cart')">Add to cart</button></div> | |
<div class="desc">Item description: {{description}}</div> | |
<img :src="img" width="100"/> | |
</div>` | |
}); | |
Vue.component('ItemsList', { | |
data() { | |
return { | |
cartItems: 0, | |
items: [ | |
{ | |
id: 1, | |
description: 'Black Shoes', | |
img: | |
'https://upload.wikimedia.org/wikipedia/commons/0/08/Shoes%2C_pair_%28black%29_%28AM_1978.53-3%29.jpg' | |
}, | |
{ | |
id: 2, | |
description: 'Funny T-Shirt', | |
img: | |
'https://farm1.staticflickr.com/42/124672109_7e972baf03_m_d.jpg' | |
} | |
] | |
}; | |
}, | |
template: `<div> | |
<div> | |
<span class="cart-icon"><img width="50" src="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/160/twitter/180/shopping-trolley_1f6d2.png"></span> | |
<span class="cart-badge">{{cartItems}}</span></div> | |
<item v-for="item in items" :description="item.description" :img="item.img" :key="item.id" @add-to-cart="cartItems++"/> | |
</div>` | |
}); | |
var app = new Vue({ | |
el: '#app', | |
template: `<items-list/>` | |
}); | |
</script> | |
</body> | |
</html> |
Author
eladcandroid
commented
Mar 31, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment