This article compares the Vue instructions v-show and v-if, introduces the difference between v-show and v-if, and talks about the usage scenarios of v-show and v-if. I hope Helpful for everyone!
1. The difference between v-show and v-if
In vue, both v-show and v-if can be used Controls whether an element is displayed on the page.
v-show's display and hiding is to operate the display attribute of the element's css, so when using v-show to hide the element, the element's dom node is still on the page; v-if Showing and hiding is to add or delete the entire dom element. [Related recommendations: vuejs video tutorial]
v-if switching has a partial compilation/uninstallation process. During the switching process, the internal Event listening and sub-components; v-show simply operates the display attribute of css.
v-if is true conditional rendering, which ensures that event listeners and subcomponents within the conditional block are destroyed and recreated appropriately during the switch. Only when the rendering condition is false, no operation is performed, and it is not rendered until it is true.
v-show will not trigger the component's life cycle when it changes from false to true. When v-if changes from false to true, it will trigger the component's beforeCreate, create, beforeMount, The mounted life cycle hook triggers the beforeDestory and destroyed methods of the component when it changes from true to false.
In terms of performance consumption, v-if has higher switching consumption; v-show has higher initial rendering consumption.
2. Usage scenarios of v-show and v-if
Both v-if and v-show can control the placement of dom elements on the page. Showing and hiding
v-if is more expensive than v-show (direct operation of adding and deleting dom nodes). If you need to switch very frequently, use v-show. Well, if the conditions rarely change during runtime, it is better to use v-if
3. Analysis of the principles of v-show and v-if
- Regardless of whether v-show is true or false, the element will always be rendered. In the source code, if the element bound to the v-show instruction has a transition nested in the outer layer, the transition will be executed. If there is no nested transition in the outer layer of el, , just set el.style.display
export const vShow: ObjectDirective<VShowElement> = { beforeMount(el, { value }, { transition }) { el._vod = el.style.display === 'none' ? '' : el.style.display if (transition && value) { transition.beforeEnter(el) } else { setDisplay(el, value) } }, mounted(el, { value }, { transition }) { if (transition && value) { transition.enter(el) } }, updated(el, { value, oldValue }, { transition }) { // ... }, beforeUnmount(el, { value }) { setDisplay(el, value) } }
- v-if is obtained for judgment when the abstract syntax tree is converted into a code string. If v-if is false , the vnode generated by the render function will not contain the node to be rendered, but an annotated vnode node as a placeholder
export const transformIf = createStructuralDirectiveTransform( /^(if|else|else-if)$/, (node, dir, context) => { return processIf(node, dir, context, (ifNode, branch, isRoot) => { // ... return () => { if (isRoot) { ifNode.codegenNode = createCodegenNodeForBranch( branch, key, context ) as IfConditionalExpression } else { // attach this branch's codegen node to the v-if root. const parentCondition = getParentCondition(ifNode.codegenNode!) parentCondition.alternate = createCodegenNodeForBranch( branch, key + ifNode.branches.length - 1, context ) } } }) } )
(Learning video sharing: vuejs tutorial, webfrontend)
The above is the detailed content of Compare the Vue instructions v-show and v-if, and talk about usage scenarios. For more information, please follow other related articles on the PHP Chinese website!

vue中props可以传递函数;vue中可以将字符串、数组、数字和对象作为props传递,props主要用于组件的传值,目的为了接收外面传过来的数据,语法为“export default {methods: {myFunction() {// ...}}};”。

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
