I am currently working on a Vue app where I need to display or hide values based on selected options from a multiselect. So far, everything is functioning correctly with the select and the corresponding function it calls.
However, I encountered an issue when trying to use includes
within my v-if statement in the HTML. It prompted an error saying it cannot read properties of undefined when trying to access includes.
I suspect there might be an issue with how I'm calling the includes method or the testArray variable here. Can anyone provide insight into this problem?
new Vue({
el: "#app",
props: {
},
components: {Multiselect: window.VueMultiselect.default},
data: {
tiers:[
{name: "test tier"}
],
selectedTier: [],
values: [
{name: "adam", tier: "test tier"},
{name: "sam", tier:"none"}
]
},
checkTier(){
console.log(this.selectedTier);
let testArray = [];
this.selectedTier.forEach(fields => {
this.testArray.push(fields.name);
});
console.log(this.testArray);
},
})
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a2c2f3f77372f362e33293f363f392e1a68746b746a">[email protected]</a>"></script>
<link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbcdcede96d6ced7cfd2c8ded7ded8cffb89958a958b">[email protected]</a>/dist/vue-multiselect.min.css">
<div id="app">
<div class="uk-width-2-10" style="position:relative;">
<multiselect
v-model="selectedTier"
:options="tiers"
:multiple="true"
:close-on-select="true"
placeholder="All Tiers"
label="name"
track-by="name"
@input="checkTier"
></multiselect>
</div>
<div v-for="value in values">
<div v-if="testArray.includes(value.tier)">
<p>Working</p>
</div>
</div>
</div>