Pretty simple question.
In my vue.js template I have:
<template v-for="node in nodes">
[[ node ]]
</template>
I want to return nodes in the list or print "N/A" if the list is empty.
I know how to do this with a js function, but not sure how to accomplish the same thing from within the template.
If you are a using core jquery and you need to check array or object is empty or not then you can do it easily. In vue js you can't do it very simple, but you can do it using v-if and array. length.
Refs are Vue. js instance properties that are used to register or indicate a reference to HTML elements or child elements in the template of your application. If a ref attribute is added to an HTML element in your Vue template, you'll then be able to reference that element or even a child element in your Vue instance.
Conclusion. To delete a property from a data object with Vue. js, we can use the this. $delete method.
Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data.
If you are a using core jquery and you need to check array or object is empty or not then you can do it easily. In vue js you can't do it very simple, but you can do it using v-if and array.length. Read Also: How to use setTimeout in Vue JS?
To check if variable is empty or null with Vue v-if and JavaScript. we can set v-if to the variable we want to check. to add the Vue script and the app container. In the mounted hook, we set this.archiveNote to 'note' which will make it truthy in the setTimeout callback which will run in 1 second after mounted is run.
to check if a list is empty, you just need to use v-if, in that way your template will be like below : Show activity on this post.
You should check for both undefined and length using Vue's v-if. If you ever use asyncComputed for example, it will be undefined until the async method resolves. So you are covered for nodes=null, nodes=undefined, and nodes= [].
You should check for both undefined
and length
using Vue's
v-if
. If you ever use asyncComputed
for example, it will be undefined
until the async method resolves. So you are covered for nodes=null
, nodes=undefined
, and nodes=[]
.
<template v-if="!nodes">
Loading
</template>
<template v-else-if="!nodes.length">
Empty
</template>
<template v-else v-for="node in nodes">
{{ node }}
</template>
Or combine them
<template v-if="!nodes || !nodes.length">
Nothing To Show
</template>
<template v-else v-for="node in nodes">
{{ node }}
</template>
I like using !nodes.length
instead of length !== 0
just in case something unexpected besides an array ends up in the data, in which case it will most likely use the empty
template and not throw an exception. Less typing too!
You can check like:
<template v-if="nodes.length"> // When there are some records available in nodes
// Iterate it and display the data
</template >
<template v-else>
// No record found
</template>
Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With