vue control element display hidden
HTML code:
<div title="Intent Price" v-if="showPrise"></div> <div title="Intentional Rent" v-show="showRentPrise"></div>
JS code:
new Vue({ el: '#app', data: { showPrise:false, showRentPrise:false } methods: { changeStatus(){ if("The conditions you set"){ = true; = true; } } } })
explain
- The default state of showPrise and showRentPrise is false, so it is hidden.
- When you pass a certain condition in changeStatus, you can control the status of showPrise and showRentPrise. true is displayed, false is hidden.
Note
- v-if controls the deletion and addition of DOM, unlike v-show's display and hide of DOM
Control elements show hidden v-show and v-if, and v-if-else
v-show and v-if function
1.v-show
- Control element display, display through display: none
- Syntax: v-show="variable or expression"
- If the variable or expression is true, the label will be displayed, otherwise it will be hidden
2. v-if
- v-if control label display
- Syntax: v-if="variable or expression"
- v-if controls whether the tag is deleted, v-if has performance advantages
3. Code examples
<template> <div> <h1 v-show="age >= 18">Years old18Only after you can see the following content</h1> <hr> <h1 v-if="age >= 18">Years old18Only after you can see the following content</h1> </div> </template>
<script> export default { data() { return { age: 1, }; }, methods: {}, }; </script> <style></style>
4.v-if-else
- v-if can be used to conditionally control display
- v-if and v-else must be adjacent elements
- v-if, v-else-if and v-else can be combined together to achieve multiple conditional judgments
- v-show cannot be used with v-else
Code Example
<template> <div> <h1 v-if="age >= 18">Come and eat durian</h1> <h1 v-else>Come and have a donut</h1> <h1 v-if="age < 18">Donut</h1> <h1 v-else-if="age < 60">Soak wolfberry in water</h1> <h1 v-else-if="age < 80">Naobaijin</h1> <h1 v-else-if="age < 100">Cordyceps sinensis</h1> <h1 v-else>A magic pill</h1> </div> </template>
<script> export default { data() { return { age: 18, }; }, methods: {}, }; </script> <style></style>
The above is personal experience. I hope you can give you a reference and I hope you can support me more.