Home  >  Article  >  Web Front-end  >  How to dynamically hide and display uniapp components

How to dynamically hide and display uniapp components

PHPz
PHPzOriginal
2023-04-23 09:12:473619browse

With the continuous development of mobile applications, developers need to be able to hide and display different components in a dynamic manner when building rich user interfaces. In uniapp, we can use dynamic property binding and conditional rendering of vue components to achieve this goal. In this article, we will look at the different ways to achieve this functionality and their pros and cons.

First, let’s take a look at the simplest and most direct method: using the v-show command. The function of the v-show directive is to determine whether the element is displayed based on the value of the expression. When the expression evaluates to true, the element will be displayed; otherwise, the element will be hidden.

In uniapp, using the v-show directive is very simple. You only need to add it to the component that needs to be hidden or displayed, and set its value to a Boolean variable. Here is an example of using the v-show directive to hide and show a button:

<template>
  <button v-show="showBtn">点击我</button>
</template>

<script>
export default {
  data() {
    return {
      showBtn: true  // 默认显示按钮
    }
  },
  methods: {
    hideBtn() {
      this.showBtn = false;  // 隐藏按钮
    },
    showBtn() {
      this.showBtn = true;   // 显示按钮
    }
  }
}
</script>

In the above example, we used the v-show directive on the button and bound its value to a data Property showBtn on. When the value of showBtn is true, the button will be displayed; when the value of showBtn is false, the button will be hidden. By calling the methods hideBtn and showBtn, we can dynamically change the value of showBtn to hide and show the button.

The advantage of the v-show directive is that it does not actually remove the element from the DOM, but rather keeps the element in the document and just hides it via CSS. This means that when we need to redisplay the element, the state of the element will be preserved. However, the disadvantage of the v-show directive is that it requires DOM manipulation every time an element is updated, which may affect performance.

The second way to hide and show elements is to use the v-if directive. Unlike the v-show directive, the v-if directive determines whether an element should exist in the DOM based on the value of an expression. When the expression evaluates to true, the element will exist in the DOM; otherwise, the element will be removed from the DOM.

In uniapp, using the v-if instruction is also very simple. Just add it to the component that needs to be hidden or shown, and set its value to a Boolean variable. Here is an example of using the v-if directive to hide and show a button:

<template>
  <button v-if="showBtn" @click="hideBtn">点击我</button>
</template>

<script>
export default {
  data() {
    return {
      showBtn: true  // 默认显示按钮
    }
  },
  methods: {
    hideBtn() {
      this.showBtn = false;  // 隐藏按钮
    },
    showBtn() {
      this.showBtn = true;   // 显示按钮
    }
  }
}
</script>

In the above example, we used the v-if directive on the button and bound its value to a data Property showBtn on. When the value of showBtn is true, the button will exist in the DOM; when the value of showBtn is false, the button will be removed from the DOM. By calling the methods hideBtn and showBtn, we can dynamically change the value of showBtn to hide and show the button.

The advantage of the v-if directive is that it only performs the necessary DOM operations each time the element is updated, which means it has less impact on performance. However, the disadvantage of the v-if directive is that when an element is deleted from the DOM, the state of this element will also be deleted. When we need to redisplay the element, we need to recreate the element and its state, which may affect performance. .

Finally, let’s look at the third method of hiding and showing elements: using the v-bind instruction to dynamically change the class of the element. By changing the class of an element, we can change the style of the element, thereby hiding and displaying the element.

In uniapp, we can bind a dynamic class to a component by using the v-bind directive. When the value of the expression is true, the component will add this class; otherwise, the component will delete this class. The following is an example of using the v-bind directive to hide and show a button:

<template>
  <button :class="{ &#39;hidden&#39;: !showBtn }" @click="hideBtn">点击我</button>
</template>

<style>
.hidden {
  display: none;
}
</style>

<script>
export default {
  data() {
    return {
      showBtn: true  // 默认显示按钮
    }
  },
  methods: {
    hideBtn() {
      this.showBtn = false;  // 隐藏按钮
    },
    showBtn() {
      this.showBtn = true;   // 显示按钮
    }
  }
}
</script>

In the above example, we use the v-bind directive to bind a dynamic class hidden to the button. When the value of showBtn is false, this class will be added to the button, thereby hiding the button; when the value of showBtn is true, this class will be deleted, thereby displaying the button. At the same time, we need to define the .hidden class in the style sheet and set the button to display: none to hide and display the elements.

Different from the v-show directive, the advantage of using the v-bind directive is that we can achieve more style changes by modifying the element's class, not just changing the element's display state. The disadvantage is that it requires using CSS to style the elements, which may have some impact on performance.

To sum up, there are many ways to hide and display elements. In uniapp, we can choose the v-show instruction, v-if instruction or use the v-bind instruction to dynamically change the class of the element. Each method has its own advantages and disadvantages, and we need to choose the most appropriate method according to specific needs and scenarios.

The above is the detailed content of How to dynamically hide and display uniapp components. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn