I'm looking for displaying bootstrap alert, if v-for return no line, it appear with v-if="!records.length", but the problem is that the alert always shown at page load!

 <tr v-for="record of records" :key="record.id">
  <td class="text-nowrap">{{ record.last_name }} {{ record.first_name }}</td>
  <td class="text-nowrap">{{ age(record) }}</td>
<div v-if="!records.length" id="errorMsg" class="alert alert-danger text-center" role="alert">
     nothing to show !

1 Answers

Matthew Vanlandingham On

As Adriano pointed out, the Vue Instance Lifecycle comes into play here. When the DOM is first created, the records data hasn't been initialized yet, meaning that !records.length will return true. To keep this from happening, initialize the records data in the created() hook. This way the data will be gathered before the DOM is rendered, so the error message will not be displayed if there are values in records.