Skip to content

Instantly share code, notes, and snippets.

@andregardi
Last active January 10, 2020 20:30
Show Gist options
  • Save andregardi/50a019648afedc8eb93579410e82c709 to your computer and use it in GitHub Desktop.
Save andregardi/50a019648afedc8eb93579410e82c709 to your computer and use it in GitHub Desktop.
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
<div id="main">
<h1>Shopping cart</h1>
<p>Apples: {{ products.apple }}</p>
<p>Bananas: {{ products.banana }}</p>
<p>Oranges: {{ products.orange }}</p>
</div>
<script>
new Vue({
el: "#main",
data: {
products: {
apple: 0,
banana: 0,
orange: 0
}
},
methods: {
add: function(event) {
this.products[event.data]++;
}
},
beforeMount() {
window.addEventListener("message", this.add);
},
beforeDestroy() {
window.removeEventListener("message", this.add);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment