I have the following html with conditional render using v-if
<div v-if="showdiv">
<p>Content 1 here</p>
</div>
<div v-if="!showdiv">
<p>Content 2 here</p>
</div>
In data variable I have set
data() {
render {
showdiv: ''
}
}
So I have an api, whose response decides whether showdiv is false or true. But sometimes api is slow, then the value of showdiv should be null and none of the div must be shown, But in my case showdiv is taken as false and this get displayed
<div v-if="!showdiv">
<p>Content 2 here</p>
</div>
I dont want both the divs to be displayed when the value is null.I only want to render the div if its true or false, in other cases let it not show. What i can do to achieve that ?
I have the following html with conditional render using v-if
<div v-if="showdiv">
<p>Content 1 here</p>
</div>
<div v-if="!showdiv">
<p>Content 2 here</p>
</div>
In data variable I have set
data() {
render {
showdiv: ''
}
}
So I have an api, whose response decides whether showdiv is false or true. But sometimes api is slow, then the value of showdiv should be null and none of the div must be shown, But in my case showdiv is taken as false and this get displayed
<div v-if="!showdiv">
<p>Content 2 here</p>
</div>
I dont want both the divs to be displayed when the value is null.I only want to render the div if its true or false, in other cases let it not show. What i can do to achieve that ?
isLoading
flag and show loading when it is true and when it is false only then render your other divs.
– Utsav Patel
Commented
May 5, 2020 at 14:11
You can check explicitly if the typeof showdiv
is boolean
or not and based on that show the divs like:
<div v-if="typeof showdiv === 'boolean' && showdiv">
<p>Content 1 here</p>
</div>
<div v-if="typeof showdiv === 'boolean' && !showdiv">
<p>Content 2 here</p>
</div>
Demo:
new Vue({
el: "#myApp",
data: {
showdiv: ''
},
async mounted() {
// wait for 2 second
await new Promise(resolve => setTimeout(resolve, 2000));
this.showdiv = true;
},
})
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.min.js"></script>
<link href="https://stackpath.bootstrapcdn./bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
<div id="myApp">
<div>
<p>The content divs will show after few seconds . . .</p>
</div>
<div v-if="typeof showdiv === 'boolean' && showdiv">
<p>Content 1 here</p>
</div>
<div v-if="typeof showdiv === 'boolean' && !showdiv">
<p>Content 2 here</p>
</div>
</div>
try like this:
<div v-if="showdiv === true">
<p>Content 1 here</p>
</div>
<div v-if="showdiv === false">
<p>Content 2 here</p>
</div>