-
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>