Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
modify dynamic data with computed properties
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js"></script>
</head>
<body>
<div id="app">
<code>{{ getProductsList | json }}</code>
</div>
<script>
var app = new Vue({
el: '#app',
mounted() {
this.getProducts();
},
data: {
products: []
},
computed: {
getProductsList() {
return this.products.map(function(product){
product.qty = 0;
return product;
});
}
},
methods: {
getProducts() {
var vm = this;
axios.get('https://jsonplaceholder.typicode.com/users').then(function(response){
vm.products = response.data;
}).catch(function(err){
alert('Something went wrong');
});
}
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment