My expectation is that when the button is clicked by the user, the data will appear according to the active category, if the sealin of these two things displays an error in the template
methods: {
fetchCategory() {
const API_URL_Categories = 'https://fakestoreapi.com/products/categories'
fetch(API_URL_Categories)
.then(response => response.json())
.then(data => {
this.categories = data
// kategori aktif ada dalam daftar kategori yang valid
// (English comment:) The active category is in the list of valid categories
if (!this.categories.includes(this.categoryActive)) {
this.categoryActive = this.categories[0]
}
// Ambil data produk berdasarkan kategori aktif
// (English comment:) Retrieve product data by active category
this.fetchProducts()
})
.catch(error => {
console.error('Error fetching categories:', error)
})
}
}