在本篇文章中,我们将学习如何在vuejs中绑定HTML属性。
这是我们的起始代码。
<template> <div> <h1>Binding atrributes in {{title}}</h1> @@##@@ </div> </template> <script> export default { data: function() { return { title: "Vuejs", image: "logo.png" }; } }; </script>
在上面的代码中,我们需要绑定src属性的数据来显示图像。
在data里面我们有image:"logo.png"属性,现在我们需要将src属性链接到image属性,以便我们可以显示图像。
立即学习“前端免费学习笔记(深入)”;
问题是花括号{{}}语法无法绑定html属性中的数据。
@@##@@ //wrong: doesn't display any image
为了绑定HTML属性中的数据,Vuejs为我们提供了一个指令v-bind:atrributename。
@@##@@ // 用户现在可以看到图像
这里v-bind指令将元素的src属性绑定到表达式image的值。
如果我们使用v-bind指令,我们可以评估引号内的JavaScript表达式v-bind:src="js expression"。
v-bind:attributename还可以编写简写语法:attributename。
<!-- fullhand syntax --> @@##@@ <!-- shorthand syntax --> @@##@@
同样,我们可以将此语法与其他HTML属性一起使用。
<button :disabled="isActive">Click</button> <a :href="url">My link</a> <div :class="myClassname"></div>
本篇文章就是关于在Vuejs中绑定HTML属性的方法介绍,希望对需要的朋友有所帮助!
以上就是如何在Vuejs中绑定HTML属性?的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号