zoukankan      html  css  js  c++  java
  • 671 vue条件渲染

    条件渲染


    v-if、v-else、v-else-if


    template元素


    v-show


    v-show和v-if的区别


    01_条件渲染的基本使用.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <body>
      
      <div id="app"></div>
    
      <template id="my-app">
        <h2 v-if="isShow">哈哈哈哈</h2>
        <button @click="toggle">切换</button>
      </template>
    
      <script src="../js/vue.js"></script>
      <script>
        const App = {
          template: '#my-app',
          data() {
            return {
              message: "Hello World",
              isShow: true
            }
          },
          methods: {
            toggle() {
              this.isShow = !this.isShow;
            }
          }
        }
    
        Vue.createApp(App).mount('#app');
    
        // JavaScript条件判断
        if (true) {
    
        }
      </script>
    </body>
    </html>
    

    02_多个条件的渲染.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <body>
      <div id="app"></div>
    
      <template id="my-app">
        <input type="text" v-model="score">
        <h2 v-if="score > 90">优秀</h2>
        <h2 v-else-if="score > 60">良好</h2>
        <h2 v-else>不及格</h2>
      </template>
    
      <script src="../js/vue.js"></script>
      <script>
        const App = {
          template: '#my-app',
          data() {
            return {
              score: 95
            }
          }
        }
    
        Vue.createApp(App).mount('#app');
      </script>
    </body>
    </html>
    

    03_template和v-if结合使用.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <body>
      <div id="app"></div>
    
      <template id="my-app">
        <template v-if="isShowHa">
          <h2>哈哈哈哈</h2>
          <h2>哈哈哈哈</h2>
          <h2>哈哈哈哈</h2>
        </template>
    
        <template v-else>
          <h2>呵呵呵呵</h2>
          <h2>呵呵呵呵</h2>
          <h2>呵呵呵呵</h2>
        </template>
      </template>
    
      <script src="../js/vue.js"></script>
      <script>
        const App = {
          template: '#my-app',
          data() {
            return {
              isShowHa: true
            }
          }
        }
    
        Vue.createApp(App).mount('#app');
      </script>
    </body>
    </html>
    

    04_v-show的条件渲染.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <body>
      <div id="app"></div>
    
      <template id="my-app">
        <h2 v-show="isShow">哈哈哈哈</h2>
      </template>
    
      <script src="../js/vue.js"></script>
      <script>
        const App = {
          template: '#my-app',
          data() {
            return {
              isShow: true
            }
          }
        }
    
        Vue.createApp(App).mount('#app');
      </script>
    </body>
    </html>
    

    05_v-show和v-if的区别.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <body>
      <div id="app"></div>
    
      <template id="my-app">
        <h2 v-if="isShow">哈哈哈哈</h2>
        <h2 v-show="isShow">呵呵呵呵</h2>
      </template>
    
      <script src="../js/vue.js"></script>
      <script>
        const App = {
          template: '#my-app',
          data() {
            return {
              isShow: true
            }
          }
        }
    
        Vue.createApp(App).mount('#app');
      </script>
    </body>
    </html>
    
  • 相关阅读:
    Ansible批量更新远程主机用户密码
    国外程序员推荐:每个程序员都应该读的非编程书
    FindFriendsServer服务搭建
    Android JNI HelloWorld实现
    2014年4月读书单
    jQuery 之父:每天写代码
    QT210 Android4.0源码编译和烧录文档整理
    Android系统分区理解及分区目录细解
    Android组件Spinner使用
    使用事件驱动模型实现高效稳定的网络服务器程序
  • 原文地址:https://www.cnblogs.com/jianjie/p/14780006.html
Copyright © 2011-2022 走看看