I have a vue component which should dispaly and handle filter-options for the results shown on the site.
The code below shows a part of my component. In this example a min and a max price can be set by the user. Because my filter is handled by the backend, I want to get all available filter-option (or the default-filters) before the component is rendered.
Unfortunatelly I get the following error-message: TypeError: Cannot read property 'min' of undefined.
COMPONENT
<template>
<div id="filter">
<fieldset>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">Preis von - bis</span>
</div>
<select class="form-control" id="min_price" v-model="filter.price.min"
v-on:change="countResults">
<option value="0" selected="selected">beliebig</option>
<option value="250">250 €</option>
<option value="500">500 €</option>
<option value="750">750 €</option>
<option value="1000">1000 €</option>
<option value="1500">1500 €</option>
</select>
<select class="form-control" id="max_price" v-model="filter.price.max"
v-on:change="countResults">
<option value="250">250 €</option>
<option value="500">500 €</option>
<option value="750">750 €</option>
<option value="1000">1000 €</option>
<option value="1500">1500 €</option>
<option value="0" selected="selected">beliebig</option>
</select>
</div>
</fieldset>
</div>
</template>
JS
<script>
export default {
name: "VehicleFilterComponent",
data: function () {
return {
categories: [],
filter: {}
}
},
methods: {
getDefaultFilter() {
axios.get(this.$apiUrl + '/vehicles/default-filters')
.then(result => {
this.filter = result.data.filter;
}).catch(function (error) {
console.warn(error)
});
}
},
beforeMount() {
this.getDefaultFilter();
}
}
</script>
Is there a way to wait until all filters are loaded?
Right now I make use of v-if and set the filter-data per default to false. So the template is only rendered when all filters where loaded.
Is there a better way, maybe via Promises?
filtertofalse) should work, why didn't you try it?data.fetched = false. From there you can render a message telling the user the data has not been fetched and another telling them it has been fetched but had no entries, etc.beforeRouteEnter: function(to, from, next)for this.