The Wayback Machine - https://web.archive.org/web/20210628230125/https://github.com/vuejs/vue/issues/11984
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
casperdai opened this issue Mar 29, 2021 · 3 comments
Open

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

casperdai opened this issue Mar 29, 2021 · 3 comments

Comments

@casperdai
Copy link

@casperdai casperdai commented Mar 29, 2021

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 ''

@posva
Copy link
Member

@posva posva commented Mar 29, 2021

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

@scientistzjf
Copy link

@scientistzjf scientistzjf commented May 2, 2021

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

@DannyFeliz
Copy link

@DannyFeliz DannyFeliz commented May 3, 2021

@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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants