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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM