在本篇文章中,我们将学习如何在vuejs中绑定html属性。
这是我们的起始代码。
<template> <div> <h1>binding atrributes in {{title}}</h1> <img src="" /> </div></template><script>export default { data: function() { return { title: "vuejs", image: "logo.png" }; }};</script>
在上面的代码中,我们需要绑定src属性的数据来显示图像。
在data里面我们有image:"logo.png"属性,现在我们需要将src属性链接到image属性,以便我们可以显示图像。
问题是花括号{{}}语法无法绑定html属性中的数据。
<img src="{{image}}" /> //wrong: doesn't display any image
为了绑定html属性中的数据,vuejs为我们提供了一个指令v-bind:atrributename。
<img v-bind:src="image" /> // 用户现在可以看到图像
这里v-bind指令将元素的src属性绑定到表达式image的值。
如果我们使用v-bind指令,我们可以评估引号内的javascript表达式v-bind:src="js expression"。
v-bind:attributename还可以编写简写语法:attributename。
<!-- fullhand syntax --><img v-bind:src="image" /><!-- shorthand syntax --><img :src="image"/>
同样,我们可以将此语法与其他html属性一起使用。
<button :disabled="isactive">click</button><a :href="url">my link</a><div :class="myclassname"></div>
本篇文章就是关于在vuejs中绑定html属性的方法介绍,希望对需要的朋友有所帮助!
以上就是如何在vuejs中绑定html属性?的详细内容。