【vue3入门】-【5】属性绑定
属性绑定
双大括号不能在html attributes中使用。想要响应的绑定一个attribute,应该使用v-bind
指令
<script>
export default {
data() {
return {
msg: "active",
myid:"test-id"
}
}
}
</script>
<template>
<div class="{{msg}}">测试</div> <!--错误用法-->
<div v-bind:id="myid" v-bind:class="msg">测试</div>
</template>
v-bind指令知识Vue将元素的id attribute与组件dynamicid属性保持一致,如果绑定的值是null或者是undefined,那么该attribute将会从渲染的元素上移除
<script>
export default {
data() {
return {
msg: "msg", // 如果值为null或者为undefined,则会将该属性从标签属性中移除
myid:"test-id",
mytitle:"my-title"
}
}
}
</script>
<template>
<div v-bind:id="myid" v-bind:class="msg" v-bind:title="mytitle">测试</div>
</template>
<style>
.active{
color:red;
font-size:20px;
}
</style>
属性绑定-简写
因为v-bind
非常常用,因此vue提供了特定的简写语法
<div :id="dynamicId" :class="dynamicClass"></div>
布尔型Attribute
布尔型Attribute依据true/false值来决定Attribute是否应该存在于该元素上,disabled就是最常见的例子之一
<script>
export default {
data() {
return {
msg: "msg",
myid:"test-id",
mytitle:"my-title",
isbuttondisable:true // true控制不可点击,false控制可点击
}
}
}
</script>
<template>
<div v-bind:id="myid" v-bind:class="msg" v-bind:title="mytitle">测试</div>
<button :disabled="isbuttondisable">按钮</button>
</template>
<style>
.active{
color:red;
font-size:20px;
}
</style>
动态绑定多个值
<style>
.active {
color: red;
font-size: 20px;
}
</style>
<template>
<div v-bind:id="myid" v-bind:class="msg" v-bind:title="mytitle">测试</div>
<button :disabled="isbuttondisable">按钮</button>
<div v-bind="objectofattrs"></div> <--使用时,直接使用封装好的属性-->
</template>
<script>
export default {
data() {
return {
msg: "msg",
myid: "test-id",
mytitle: "my-title",
isbuttondisable: true,
objectofattrs:{ //直接囊括多类属性
myid: "test-id",
mytitle: "my-title",
}
}
}
}
</script>
以上内容出自
【【2023最新版】Vue3从入门到精通,零基础小白也能听得懂,写得出,web前端快速入门教程】 https://www.bilibili.com/video/BV1Rs4y127j8/?share_source=copy_web&vd_source=94c3d5330a46438059359e8dd2494fe9
分类:
前端 / vue-入门
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!