Hey everyone, I recently attempted to set up this layout:
https://i.sstatic.net/WbcBX.png
This company offers a variety of services grouped into different categories, each containing sub-options and specific details.
This is how my JSON data is structured:
{
"services": [{
"name": "COVID-19",
"options": [
"COVID-19 Temperature Screening System"
],
"information": {
"images": [
"http://placekitten.com/300/300"
],
"description": "Lorem ipsum dolor sit amet consectetur, adipisicing elit. Qui, in."
}
},
{
"name": "Home",
"options": [
"Camera",
"Fire Alarm",
"Motion Sensor"
],
"information": {
"images": [
"http://placekitten.com/300/300"
],
"description": "Lorem ipsum dolor sit amet consectetur, adipisicing elit. Qui, in."
}
},
....
The main issue I'm facing is figuring out how to utilize a v-if statement alongside a v-for loop to dynamically change the displayed product based on user selection. I encountered this error message:
26:11 error The 'this.JSON' expression inside 'v-for' directive should be replaced with a computed property that returns filtered array instead. You should not mix 'v-for' with 'v-if'
Could anyone provide guidance on using a computed property for this purpose as I am uncertain about its meaning? Here's the snippet related to the services page:
<template>
<div class="services">
<div class="header services__header">
<h1 :class="$mq">Services</h1>
</div>
<div class="services__services-container">
<div class="services__services-container__category-selection">
<!-- GENERATE CATEGORIES -->
<div
class="services__services-container__category-selection__category"
v-for="(item, index) in this.JSON"
:key="`category-${index}`"
:class="$mq"
>
<input type="radio" :name="item.name" :value="item.name" v-model="currentCategory" />
<label :for="item.name">{{item.name}}</label>
</div>
</div>
<!-- GENERATE SERVICES -->
<div class="services__services-container__service-selection">
<div
class="services__services-container__service-selection__service"
v-for="(item, index) in this.JSON"
:key="`service-${index}`"
:class="$mq"
v-if="currentCategory === item.name"
>
<!-- ^^^ THIS V-IF IS NOT ALLOWED -->
<div
class="services__services-container__service-selection__service__wrapper"
v-for="(option, index) in item.options"
:key="`option-${index}`"
:class="$mq"
>
<input type="radio" :name="option" :value="option" v-model="currentService" />
<label :for="option">{{option.optionName}}</label>
</div>
</div>
</div>
<!-- GENERATE DESCRIPTIONS -->
<div class="services__services-container__product-description">
<div
class="services__services-container__product-description__description"
v-for="(item, index) in this.JSON"
:key="`service-${index}`"
:class="$mq"
>
<div v-for="(option, index) in item.options" :key="`option-${index}`" :class="$mq">
<img :src="option.images" alt />
<p>{{option.description}}</p>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import services from "@/JSON/services.json";
export default {
name: "Services",
data: function() {
return {
JSON: [],
currentCategory: "Home",
currentService: ""
};
},
created: function() {
//TO TEST
this.JSON = services.services;
this.JSON.forEach(element => {
console.log(element);
});
}
};
</script>
<style lang="scss">
@import "@/scss/variables.scss";
@import "@/scss/button.scss";
@import "@/scss/header.scss";
@import "@/scss/input.scss";
.services {
width: 100%;
height: auto;
display: grid;
grid-template-rows: 15vh auto auto auto;
&__services-container {
padding: 2rem;
& input {
margin-right: 0.5rem;
}
&__category-selection {
background-color: $colour-green;
padding: 1rem;
margin-bottom: 1rem;
}
&__service-selection {
padding: 1rem;
margin-bottom: 1rem;
&__service {
background-color: $colour-blue;
margin-bottom: 1rem;
}
}
&__product-description {
background-color: $new-service;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
&__description {
& img {
margin-bottom: 1rem;
}
background-color: pink;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
text-align: center;
}
}
}
}
</style>
Script portion:
<script>
import services from "@/JSON/services.json";
export default {
name: "Services",
data: function() {
return {
JSON: [],
currentCategory: "Home",
currentService: ""
};
},
created: function() {
//TO TEST
this.JSON = services.services;
this.JSON.forEach(element => {
console.log(element);
});
}
};
</script>