Home > Web Front-end > Vue.js > body text

How to use the v-if conditional directive in the Vue documentation

王林
Release: 2023-06-21 10:09:18
Original
2534 people have browsed it

Vue is an open source JavaScript framework for building special user interfaces. The v-if conditional directive in the Vue documentation is a way to control whether a Vue element is rendered. Using the v-if directive, you can control whether to render elements based on data in the page.

The v-if directive can accept a Boolean expression. If the result of the expression is true, Vue will render the element. Otherwise, Vue will not render the element to the page. The following is the syntax of the v-if instruction:

<element v-if="expression"></element>
Copy after login

expression is a JavaScript expression, which can be a variable, function, or calculation.

The following is an example of the v-if directive:

<template>
  <div>
    <p v-if="show">这里是文本内容</p>
    <button @click="toggleShow">点击切换文本内容</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: true
    };
  },
  methods: {
    toggleShow() {
      this.show = !this.show;
    }
  }
};
</script>
Copy after login

In the above example, when the value of the show variable is true, Vue will render the p element and display "Here is the text content" ;When the value of the show variable is false, Vue will not render the p element. The toggleShow method can change the value of the show variable by clicking the button, thereby switching the display and hiding of text content.

In addition to the v-if directive, Vue also provides the v-show directive. The usage of the v-show directive is similar to the v-if directive, but the v-show directive does not remove elements from the DOM, but uses CSS to control the display and hiding of elements. Therefore, the v-show instruction is more efficient than the v-if instruction.

If you need to render a group of elements, you can use the v-for directive in combination with the v-if directive. The following is the syntax of the v-for directive combined with the v-if directive:

<element v-for="item in items" v-if="expression"></element>
Copy after login

expression is a JavaScript expression that controls whether the element is rendered. The following is an example of using the v-for and v-if directives together:

<template>
  <ul>
    <li v-for="(item, index) in items"
        v-if="item.visible"
        :key="index">
      {{ item.text }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { text: '第一项', visible: true },
        { text: '第二项', visible: false },
        { text: '第三项', visible: true }
      ]
    };
  }
};
</script>
Copy after login

In the above example, Vue will render the elements whose visible attribute value is true in the items array, that is, the first and third items .

Use the v-if directive to more flexibly control the display and hiding of Vue elements. However, it should be noted that in some cases, the v-if instruction may cause performance problems, so the choice needs to be made on a case-by-case basis.

In short, the v-if conditional instruction is a very commonly used instruction in the Vue framework. Mastering its usage can allow developers to better control page rendering and improve user experience.

The above is the detailed content of How to use the v-if conditional directive in the Vue documentation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template