I have a json response from an laravel api with 800 items on it. I wish to show 15 items to the user. The user ll have to click 'Load More' button to show more 15.
Everything is working as expected but Vue Js throws this warning :
[Vue warn]: Error in render: "TypeError: Cannot read property 'slice' of undefined"
Code:
<div class="col" v-for="value in products.products.slice( 0, productsShow)">
//logic {{value.content}}
</div>
<button
v-if="products.products.length > 15 &&
productsShow < products.products.length"
@click="loadMore">
Load more products
</button>
VueJs
<script>
import axios from 'axios'
export default {
data() {
return {
products: [],
productsShow: ''
}
},
methods: {
loadMore () {
this.productsShow += 15
}
},
created() {
axios.get('/api/products/pt').then(response => this.products = response.data)
this.productsShow = 15
}
}
</script>
Also Tried this :
<script>
import axios from 'axios'
export default {
data() {
return {
products: [],
productsShow: 15
}
},
methods: {
loadMore () {
this.productsShow += 15
}
},
created() {
axios.get('/api/products/pt').then(response => this.products = response.data)
}
}
</script>
Edit Api response : Api Laravel Response
products
is an array not an object, so you can not doproducts.products
. – Aliciaproducts.slice( 0, productsShow)
instead ofproducts.products.slice( 0, productsShow)
? – Terry