vue icon indicating copy to clipboard operation
vue copied to clipboard

<v-show> style.display is incorrect when reusing the elm

Open casperdai opened this issue 3 years ago • 6 comments

Version

2.6.11

Reproduction link

https://jsfiddle.net/CasperDai/mred18vo/7/

Steps to reproduce

click 'change'

What is expected?

el.style.display is 'flex'

What is actually happening?

el.style.display is ''

casperdai avatar Mar 29 '21 10:03 casperdai

Use a key on the <div> as a workaround.

posva avatar Mar 29 '21 11:03 posva

Use a key on the <div> as a workaround. Excuse me, how should I operate?

scientistzjf avatar May 02 '21 13:05 scientistzjf

@scientistzjf

It’s usually best to use key with v-if + v-else, if they are the same element type (e.g. both <div> elements).

By default, Vue updates the DOM as efficiently as possible. That means when switching between elements of the same type, it simply patches the existing element, rather than removing it and adding a new one in its place. This can have unintended consequences if these elements should not actually be considered the same.

Good

<div
  v-if="error"
  key="search-status"
>
  Error: {{ error }}
</div>
<div
  v-else
  key="search-results"
>
  {{ results }}
</div>

Bad

<div v-if="error">
  Error: {{ error }}
</div>
<div v-else>
  {{ results }}
</div>

DannyFeliz avatar May 03 '21 21:05 DannyFeliz

@scientistzjf

use key like below

<div id="app">
  <div>
    <div @click="click">change</div>
    <div v-if="state === 0">
      <div key='first-key' v-show="hasChildren">some data</div>
    </div>
    <div v-else>
      <div ref="t" key='second-key' style="display: flex;">display: {{ val }}</div>
    </div>
  </div>
</div>

dineshrabara avatar Jul 05 '21 12:07 dineshrabara

Is any working solution??

b8x avatar Sep 10 '21 13:09 b8x

Use a key on the div

DaZuiZui avatar Jan 22 '23 15:01 DaZuiZui