01. V – show and v – if
(1) Difference
-
V-if is true conditional rendering and does not start rendering until the condition is true for the first time (lazy loading)
- The V-if directive destroys and rebuilds DOM nodes directly, allowing elements to be shown and hidden
-
Will v-show render regardless of initial conditions and simply switch based on the display property of CSS
v-show
CSS level onlydisplay: none;
和display: block;
Switching between
(2) Usage scenarios
v-if
Suitable for scenes that do not require frequent condition switching (render in one go)- Use trigger component lifecycles on components
v-show
Is suitable for scenarios that require very frequent switching conditions- Not available for components
(3) Performance optimization
- How to use
v-if
Performance tuning?- Because when
v-if="false"
Internal components are not rendered - So when certain conditions render part of a component (or content), you can set the conditions to
false
- Set to if necessary
true
(or through asynchronous operations such as$nextTick
) - This gives priority to rendering other important content, which can be used wisely for performance optimization
- Because when
02. V – if and v – for
You should avoid using v-if in V-for
(1) Priority
- 1.
v-for
Prior to thev-if
Be parsed - 2. If it happens at the same time, each render will execute a loop before judging the condition, so the loop is inevitable anyway, wasting performance
- 3. To avoid this, nest in the outer layer
<template>
, on this floorv-if
Judge, and then do it internallyv-for
cycle - 4. If the condition is present inside the loop, you can filter out the items that do not need to be displayed in advance by calculating the attributes
(2) About key in V-for
-
Why use
key
:- The main purpose is to update the virtual DOM efficiently
- use
key
To add one to each element nodeA unique identifier - Can be convenient
Vue
betterThe difference betweenThe components,The Diff algorithmYou canCorrect identificationThis node
When using V-for to update a rendered element list, the default is in-place reuse
- When the list data is modified, it will be based on
key
Value to determine whether a value is modified - If modified, re-render this item
- Otherwise, reuse the previous element
-
Try not to use index as key:
- This is due to
index
It’s always sequential - If you insert a piece of data, it will cause subsequent data
index
All values change - This will cause all subsequent data to be re-rendered
- This is due to
I front-end side dish chicken, if there is wrong, please forgive