Vue: about use v-if image is loaded

Created on 29 Oct 2015  ·  7Comments  ·  Source: vuejs/vue

<div v-if="condition">
    <img  src="path" />
</div>

In the above code, if the condition is satisfied, it will insert the image. If condition is not satisfied, it would not insert the image. But in the network panel has seen this image is loaded

Most helpful comment

Unfortunately this happens when Vue clones the template, even if the node is off-DOM it will still trigger the network request. This happens before Vue compiles it.

A simple workaround is use :src="'path'". This should not trigger any request until it is compiled.

All 7 comments

Proof?

The image dom node is not in the nodes tree, but we can see the image load request in the chrome network panel

By proof we mean a reproduction in a jsfiddle or jsbin

Proof: http://jsfiddle.net/4z02btnp/

You can also see directly below the screenshot:

This is most likely because the browser attempts to download the image before Vue gets a chance to remove the <img> element from the DOM. Try wrapping the template in <template> or <script>, or even inlining it into js.

Unfortunately this happens when Vue clones the template, even if the node is off-DOM it will still trigger the network request. This happens before Vue compiles it.

A simple workaround is use :src="'path'". This should not trigger any request until it is compiled.

ok,thx

Was this page helpful?
0 / 5 - 0 ratings

Related issues

karevn picture karevn  ·  42Comments

RashadSaleh picture RashadSaleh  ·  51Comments

okjesse picture okjesse  ·  49Comments

yyx990803 picture yyx990803  ·  48Comments

eu81273 picture eu81273  ·  40Comments