功能:
當點擊按鍵時,改變當前循環數組里的status里的值,
判斷staus
里的當前的值來,切換顯示 刪除
和 恢復
的按鈕
判斷staus
里的當前的值來改變span標簽
里的字體顏色樣式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>vue</title>
<link rel="stylesheet" href="">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- <script type="text/javascript" src="../js/vue.js"></script> -->
<style type="text/css">
.success{
color: green;
}
.error{
color:red;
}
</style>
</head>
<body>
<div id="vue">
<!--循環app里的news-->
<li v-for="v in news">
<!--當前數組v里的status為ture是就用sucess樣式,否則用error里的樣式-->
<span :class="v.status?'success':'error'">{{v.title}}</span>
<!--點擊時把當着數組傳給changeStatus方法,並把要改變的值也傳去,讓其在方法里改變當前數組里的值-->
<button @click="changeStatus(v,false)" v-if="v.status">刪除</button> <!--判斷有status時才顯示按鈕-->
<button v-on:click="changeStatus(v,true)" v-if="!v.status">恢復</button>
</li>
</div>
</body>
<script type="text/javascript">
var app=new Vue({
el:'#vue',
methods:{
changeStatus(item,status){
console.log(item); //item代表傳過來循環的當前數組
console.log(status); //要改變的狀態
item.status=status; //把當前數組里的status重新賦值
}
},
data:{
news:[
{title:'haha',status:true},
{title:'hehe',status:false},
],
},
});
</script>
</html>
效果: