@用Vue组件实现购物车复选框功能模块

生命周期

  • Vue实例存活的一个过程
  • 创建(Vue实例)
  • 前 beforeCreate
  • 后 created
  • 挂载(Dom没有在页面上显示)
  • 前 beforeMount
  • 后 mounted
  • 更新(data)
  • 前 beforeUpdated
  • 后 updated
  • 销毁(Vue实例)
  • 前:beforeDestroy
  • 后:destroyed
  • created
  • 去改变data中的数据
  • 在这个生命周期函数中获取数据
  • mounted
  • 在这做页面渲染完成后的操作

数组/对象的数据响应式

  • Vue.set
  • vm.$set
  • 数组(通过索引改变数组)
  • vm.$set([数组],[索引],[值])
  • 对象 (对象中添加新属性)
  • vm.$set(【对象】,【key】,【值】)

组件简单数据交互(兄弟)

  • 单独的事件中心管理组件间的通信
  • var eventHub = new Vue()
  • `var cart1={
  •  		data:function(){
     		return {status:false}
     	},
     	template:`<a href="" class="del" @click.prevent="del(item.id)">删除</a>`
     	methods:{
     			handle:function(){
     					console.log(this.status)
     					hub.$emit('theadcbx',status)
     				}
     	},
     	mounted:function(){
     			// 监听自己的事件  val属于兄弟组件传来的值
     			hub.$on('tbodycbx',(val)=>{
     				console.log(val)
     				this.status=val
     			})
     		}
    

}`

var cart2={
			data:function(){
				return {
					state:false
				}
			},
			methods:{
					handle2:function(){
					console.log(this.state)
					hub.$emit('tbodycbx',this.state)
				}
			},
			template:`
					<tr>
						<th class="col0">
							<input type="checkbox" v-model="state" @change="handle2" id="selectAll"/>
						</th>
						<th class="col1">商品名称</th>
						<th class="col2">商品信息</th>
						<th class="col3">单价</th>
						<th class="col4">数量</th>
						<th class="col5">小计</th>
						<th class="col6">操作</th>
					</tr>
				`,
			mounted:function(){
				hub.$on('theadcbx',(val)=>{
					console.log(val)
				})
			}
	}

实现了 cart1中的 theadcbx 正常触发了car2 中的 tbodycbx

  • 也就是全选控制体复选框

但是未实现体复选框影响全选复选框 正常逻辑是要判断下勾选的数量是否等于循环的数组长度;

  • 如果数量相等则勾选全选按钮 ,否则取消。
  • 但是体复选框在勾选时出现了问题,随机选中一个,其他兄弟复选框页跟随选中。暂时未想到好的办法。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值