SoFunction
Updated on 2025-04-05

Use vue to control the display of hidden elements

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

&lt;template&gt;
  &lt;div&gt;
    &lt;h1 v-if="age &gt;= 18"&gt;Come and eat durian&lt;/h1&gt;
    &lt;h1 v-else&gt;Come and have a donut&lt;/h1&gt;
    &lt;h1 v-if="age &lt; 18"&gt;Donut&lt;/h1&gt;
    &lt;h1 v-else-if="age &lt; 60"&gt;Soak wolfberry in water&lt;/h1&gt;
    &lt;h1 v-else-if="age &lt; 80"&gt;Naobaijin&lt;/h1&gt;
    &lt;h1 v-else-if="age &lt; 100"&gt;Cordyceps sinensis&lt;/h1&gt;
    &lt;h1 v-else&gt;A magic pill&lt;/h1&gt;
  &lt;/div&gt;
&lt;/template&gt;
<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.