欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > Vue学习笔记(二)

Vue学习笔记(二)

2024/10/24 21:51:49 来源:https://blog.csdn.net/qq_73340809/article/details/143216457  浏览:    关键词:Vue学习笔记(二)

在这里插入图片描述

属性绑定

双大括号不能再HTML attributes中使用。想要响应式地绑定一个attributte,应该使用v-bind指令

<script>export default {data() {return {dynamicClass: "appclass",dynamicId: "appId",dynamicTitle: null}}}
</script><template><div v-bind:id="dynamicId" v-bind:class="dynamicClass" v-bind:title="dynamicTitle">测试</div>
</template><style>.appclass{color: red;font-size: 30px;
}
</style>

v-bind指令指示Vue将元素的idattribute与组件的dybanucId属性保持一致。如果绑定的值是null或者undefined,那么该attribute将会从渲染的的元素上移除。

20241024181245

简写

因为v-bind非常常用,我们提供了特定的简写语法:,即直接将v-bind省去

 <div :id="dynamicId" :class="dynamicClass" :title="dynamicTitle">测试</div>

布尔型attribute

布尔型attribute依据true/false值来决定attribute是否应该存在于该元素上,disabled就是最常见的例子之一。

<script>export default {data() {return {dynamicClass: "appclass",dynamicId: "appId",dynamicTitle: undefined,isButtonDisabled: false}}}
</script><template><div :id="dynamicId" :class="dynamicClass" :title="dynamicTitle">测试</div><button :disabled="isButtonDisabled">Button</button>
</template><style>.appclass{color: red;font-size: 30px;
}
</style>

动态绑定多个值

如果你有像这样的一个包含多个attribute的JavaScript对象

<script>export default {data() {return {dynamicClass: "appclass",dynamicId: "appId",dynamicTitle: undefined,isButtonDisabled: false,objectOfAttrs: {id: "appId",class: "appclass"}}}}
</script><template><div :id="dynamicId" :class="dynamicClass" :title="dynamicTitle">测试1</div><button :disabled="isButtonDisabled">Button</button><div v-bind:="objectOfAttrs">测试2</div>
</template><style>.appclass{color: red;font-size: 30px;
}
</style>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com