vue $set的用法

<template>
	<div class="list">
		<table>
			<tr v-for="item in orderList">
				<td :class="{active:item.checked}">
					<span >{{item.id}}</span>
					<span>{{item.name}}</span>
					<button @click="selectHandle($index)">点击</button>
				</td>
			</tr>
		</table>
	</div>
</template>
<script>
	export default{
	   route:{
        data (transition){
            let query = transition.to.query,tab = query.tab || 'all';
            console.log(query);
            console.log(transition.to.params);
         }
       },
       data(){
       	  return {
       	  	  orderList:[
 				{"id":1,"name":"hh01"},
 				{"id":2,"name":"hh02"}
       	  	  ]
       	  }
       },
       methods:{
       	  selectHandle(index){
       	  	  //下面实现方式是vue1.0 实现方式 vm.$set( keypath, value ) 这里只能接受两个参数 因此下面只能通过传递索引index来实现
//如果是vue2.0 可以传item(object) 进来  这样就方便多了  this.orderList[index] 可以换成 item就可以了  vue2.0中 vm.$set( object, key, value ) 和 vue1.0 vm.$set( keypath, value ) 不一样 
//下面vue2.0可以改成 this.$set(item,checked,true);
//vue1.0  只能通过索引  this.$set('orderList['+index+']["checked"]',true); 来实现
        this.$set('orderList['+index+']["checked"]',true);
         	 if(typeof this.orderList[index].checked == 'undefined'){
       	  	 	this.$set('orderList['+index+']["checked"]',true);
       	  	 	alert(this.orderList[index].checked)
       	  	 }else{
       	  	 	this.orderList[index].checked = !this.orderList[index].checked;
       	  	 }
             
       	  }
       },
       ready(){
       	   // var xx = this.$set('orderList[0]["checked"]',true);
       	   // console.log(this.orderList);
       }
	}
</script>
<style lang="sass">
    .list table{
    	width:100%;
    	max-width:640px;
    	margin:40px auto;
    	margin:0;
    	padding:0;
    	font-size:16px;
    }
	.list td,.list tr{
		border:1px solid #ccc;
	}
	.active{
		color:#f00;
	}
</style>

  

转载于:https://www.cnblogs.com/zhujiasheng/p/6726253.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值