I am trying to hide the vue.js template's contents before it is fully rendered. Consider following template:
<div class="loader">
<table class="hidden">
<tr v-for="log in logs">
<td>{{log.timestamp}}</td>
<td>{{log.event}}</td>
</tr>
</table>
</div>
When I render this template on the server, the user sees the contents of the <tr>
element before it is rendered. For this reason I use the class hidden
to hide it, before it is fully rendered.
Also before it is rendered, I am showing a loader element with some animated progressbar.
Once it is rendered, I would just call element.show()
in jQuery and hide the progressbar as well. My question is: is it okay to mix jQuery and vue.js to achieve this?
var vueLogs = new Vue({
el: "#checkInLogsHolder",
data: {logs: []}
});
var holder = $("#checkInLogsHolder");
function response(payload) {
// hiding the loader animation
holder.find(".loader").remove();
// showing the rendered table
holder.find("table").show();
vueLogs.logs.unshift(payload);
}
Is there some better way to do this?
Vue already has a
v-cloak
directive built in, you just need to add the relevent css class:And then apply it to your element like so:
Here's the JSFiddle: https://jsfiddle.net/2jbe82hq/
If you remove
v-cloak
in that fiddle you will see the mustached{{message}}
before the instance has been mounted.If you want to display a
loader
while you retrieve data from your server, then you combine a boolean flag withv-if
to show and hide the loader:You can then do:
Here's the JSFiddle for that: https://jsfiddle.net/fa70phLz/
It's also possible to use a
loading
class as well and then usev-bind:class
to apply and remove the class based on the flag, which is useful if you want to overlay the entire page with aloader
.I'm just adding a useful things of v-cloak to add a spinner before rendering the full HTML file and thanks to Adam for his Gist - https://gist.github.com/adamwathan/3584d1904e4f4c36096f
v-cloak
inside#app
.v-cloak--inline
which we want to show before HTML page rendered.v-cloak--hidden
.Let's code - In Master Page,
With adding these Extra CSS's for v-cloak.
Then Before compiling the HTML file, a spinner will render. After compiled, spinner will hide. You can use this in your master page. Thus in every time when you load a new page, that will see the effects.
See the Gist - https://gist.github.com/adamwathan/3584d1904e4f4c36096f
By adding v-cloak to whole page root div will work
Hide elements during loading using "v-cloak"
I would hesitate to mix jQuery in Vue.js code for the reason that Vue.js provides a suite of tools that generally eliminate the need for jQuery.
Since you are loading the page and displaying a progress bar, you can take advantage of the Vue.js lifecycle hooks: [created, mounted, beforeUpdate, updated, etc. etc.].
What I would do is to create a small div containing the progress bar, and then an adjacent element with the actual content to display after load. Set a data on the component instance, and then use the lifecycle hooks to toggle the display.
Then in the script part of the template:
Using this method, you can also bind classes to the elements to create smooth CSS transitions.
Other than
v-cloak
. You could also usev-text
attribute. So you don't have to deal with it in CSS.