Vue —— if-else条件渲染

  • v-if
  • v-else-if
  • v-else
判断vue.js的变量的值,然后执行页面渲染逻辑(if-elseif-else)
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>if_else.html</title>
    <script src="../js/vue.js"></script>
    <link type='text/css' rel='styleSheet' href='../css/style.css' >
</head>

<body>
    <div id="example">
        <!-- if-else情况下,为同一个地方显示,这边是同一个h3标签,换句话说就是只会触发一个标签显示 -->
        <h3 v-if="score == 0">请先点击查询成绩</h3>
        <h4 v-else-if="score < 60">{{score}}分,考试不及格</h3>
        <h3 v-else-if="score < 80">{{score}}分,继续努力</h3>
        <h2 v-else-if="score < 95">{{score}}分,考的不错</h3>
        <h1 v-else>{{score}}分,你真是太棒了!</h3>
        <!-- 点击查询成绩 -->
        <button @click="btnclick()">查询成绩</button>
    </div>
</body>
<script>
    new Vue({
        el: "#example",
        data: {
            score: 0
        },
        methods: {
            btnclick: function() {
                //Math.random()将随机生成0-1的double小数
                this.score = Math.round(Math.random()*100);
            }
        }
    });

</script>
</html>

 

posted @ 2020-10-24 09:29  a最简单  阅读(4398)  评论(0)    收藏  举报