Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding vue.js template before it is rendered

Tags:

vue.js

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?

like image 216
Vojtěch Avatar asked Feb 15 '17 23:02

Vojtěch


People also ask

How do I hide the VueJS syntax while the page is loading?

You can use the v-cloak directive, which will hide the Vue instance until the compilation finishes, if you combine it with the right CSS.

How do I make my Vue component invisible?

To hide the element but keep its space use :class="{ invisible: ! value }" to assign invisible class (which has visibility: hidden style applied to it).

What is V-cloak?

The v-cloak directive is a Vue. js directive that will remain on the element until the associated Vue instance finishes compilation. Combined with CSS rules such as [v-cloak] { display: none }, this directive can be used to hide uncompiled mustache bindings until the Vue instance is ready.

What is conditional rendering in Vue?

Conditional rendering refers to the ability to render distinct user interface (UI) markup based on whether a condition is true or not. This notion is frequently used in contexts like showing or hiding components (toggling), switching application functionality, handling authentication and authorization, and many more.


1 Answers

Vue already has a v-cloak directive built in, you just need to add the relevant css class:

[v-cloak] {   display: none; } 

And then apply it to your element like so:

<div v-cloak>   {{message}} </div> 

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 with v-if to show and hide the loader:

var vm = new Vue({   el: "#app",   created(){     this.$http.get('url').then(response => {       // set loader to false       this.loading = false;     });   },   data: {     message: 'Hello Vue!',     loading: true   } }); 

You can then do:

<div v-if="loading">    Loading... </div> <div v-else>     {{message}} </div> 

Here's the JSFiddle for that: https://jsfiddle.net/fa70phLz/

It's also possible to use a loading class as well and then use v-bind:class to apply and remove the class based on the flag, which is useful if you want to overlay the entire page with a loader.

<div :class="{'loading': loading}"></div> 
like image 164
craig_h Avatar answered Oct 20 '22 20:10

craig_h