Vue基础知识梳理(二)

本文详细梳理了Vue中的动态绑定class、.native事件监听、子组件调用父组件方法、处理非响应式数据、keep-alive的使用以及router-view的命名视图功能。介绍了v-bind:class的两种实现方式、.native修饰符解决组件事件穿透问题,以及通过this.$parent访问父组件数据。还探讨了如何避免不需要响应式的数据处理,并解析了keep-alive在组件缓存中的作用,以及router-view在不同层级路由中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue基础知识梳理(二)

1. vue中v-bind:class动态添加class

总结:动态添加class,需要给:class=“对象”,对象中value为true,则key作为class名添加;value为false,则添加失败

  • 方案一:直接在html结构中给class一个对象
//html
<div class="btn" :class="{wordColor:false,wordSize:true}" >点我</div>

//样式
.btn{
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    background: cornflowerblue;
}
.wordColor{
    color: crimson;
}
.wordSize{
    font-size: 24px;
}

效果
在这里插入图片描述

  • 方案二:使用计算属性返回对象
//html
 <div class="btn" :class="classname" >点我</div>

//计算属性
computed:{
	//返回一个对象给class,对象中属性值为true的属性,属性名被添加到class名中
    classname(){
        return{
            wordColor:true,
            wordSize:false
        }
    }
}
//样式
.btn{
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    background: cornflowerblue;
}
.wordColor{
    color: crimson;
}
.wordSize{
    font-size: 24px;
}

效果图
在这里插入图片描述

  • 动态添加style也是一样
动态style对象:<div :style="{ color: textColor, fontSize: '18px' }">&l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值