08.02.2017 Views

vuejs

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A Flavor of Directives. 18<br />

v-else in action<br />

Just for the sake of the example we have used an h2 tag with a different warning than before which<br />

is displayed conditionally. If there is no message present, we see the h1 tag. If there is a message, we<br />

see the h2 using this very simple syntax of Vue v-if and v-else. As you can see above we’ve used<br />

v-if as well as v-show. Both give us the same result. Simple as a pimple!<br />

3.4 v-if vs. v-show<br />

Even though we have already mentioned a difference between v-if and v-show , we can deepen a bit<br />

more. Some questions may arise out of their use. Is there a big difference between using v-show and<br />

v-if? Is there a situation where performance is affected? Are there problems where you’re better<br />

off using one or the other? You might experience that the use of v-show on a lot of situations causes<br />

bigger time of load during page rendering. In comparison, v-if is truly conditional according to the<br />

guide of Vue.js.<br />

When using v-if, if the condition is false on initial render, it will not do anything<br />

- partial compilation won’t start until the condition becomes true for the first time.<br />

Generally speaking, v-if has higher toggle costs while v-show has higher initial render<br />

costs. So prefer v-show if you need to toggle something very often, and prefer v-if if the<br />

condition\ is unlikely to change at runtime.<br />

So, when to use which really depends on your needs.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!