Loading...
Loading...
By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
Attribute bindings in Vue.js allow dynamically setting HTML attribute or component prop values based on data. For this v-bind directive or its short syntax : is used.
<a v-bind:href="url">Link</a>
<a :href="url">Link</a>
<template>
<button
:disabled="isLoading"
:title="tooltipText"
v-bind:data-id="item.id"
>
{{ isLoading ? "Loading..." : "Submit" }}
</button>
</template>
<script>
export default {
props: {
url: String,
item: Object,
isLoading: Boolean,
tooltipText: String
}
};
</script>
Don't confuse with regular string:
Writing <button disabled="isLoading"> won't work — without v-bind or : Vue won't interpret value as variable.
<template>
<img v-bind="imageAttrs" />
</template>
<script>
export default {
data() {
return {
imageAttrs: {
src: "logo.png",
alt: "Logo",
width: 100,
height: 50
}
};
}
};
</script>
Note:
Vue will automatically distribute all fields of imageAttrs object as <img> attributes.
In component data or props set variables for attributes.
Add :attribute="expression" to needed element.
Write :href="url", not :href="'url'" — otherwise it becomes string.
Note:
For binding classes and styles there are separate directives :class and :style, but they're also implemented via v-bind.
Learn more in official Vue documentation: Attribute Binding