在本篇文章中,我们将学习如何在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属性?的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |