v-bind
DirectiveYou have already seen that a basic Vue setup consists of a Vue instance and that we can access it from the <div id="app">
tag with {{ }}
or the v-bind
directive.
On this page we will explain the v-bind
directive in more detail.
v-bind
DirectiveThe v-bind
directive lets us bind an HTML attribute to data in the Vue instance. This makes it easy to change the attribute value dynamically.
<div v-bind:[
attribute]="[
Vue data]"></div>
The src
attribute value of an <img>
tag is taken from the Vue instance data property 'url':
<img v-bind:src="url">
Try it Yourself »
We can use the v-bind
directive to do in-line styling and modify classes dynamically. We will show you briefly how to do that in this section, and later in this tutorial, on the CSS Binding page, we will explain this in more detail.
In-line styling with Vue is done by binding the style attribute to Vue with v-bind
.
As a value to the v-bind directive, we can write a JavaScript object with the CSS property and value:
The font size depends on the Vue data property 'size'.
<div v-bind:style="{ fontSize: size }">
Text example
</div>
Try it Yourself »
We can also separate the font size number value from the font size unit if we want to, like this:
The font size number value is stored the Vue data property 'size'.
<div v-bind:style="{ fontSize: size + 'px' }">
Text example
</div>
Try it Yourself »
We could also write the CSS property name with CSS syntax (kebab-case) in hyphens, but it is not recommended:
The CSS property fontSize is referred to as 'font-size'.
<div v-bind:style="{
'font-size': size + 'px' }">
Text example
</div>
Try it Yourself »
The background color depends on the 'bgVal' data property value inside the Vue instance.
<div v-bind:style="{ backgroundColor: 'hsl('+bgVal+',80%,80%)' }">
Notice the background color on this div tag.
</div>
Try it Yourself »
The background color is set with a JavaScript conditional (ternary) expression depending on whether the 'isImportant' data property value is 'true' or 'false'.
<div v-bind:style="{ backgroundColor: isImportant ? 'lightcoral' : 'lightgray' }">
Conditional background color
</div>
Try it Yourself »
We can use v-bind
to change the class attribute.
The value of v-bind:class
can be a variable:
The class
name is taken from the 'className' Vue data property:
<div v-bind:class="className">
The class is set with Vue
</div>
Try it Yourself »
The value of v-bind:class
can also be an object, where the class name will only take effect if it is set to 'true':
The class
attribute is assigned or not depending on if the class 'myClass' is set to 'true' or 'false':
<div v-bind:class="{ myClass: true }">
The class is set conditionally to change the background color
</div>
Try it Yourself »
When the value of v-bind:class
is an object, the class can be assigned depending on a Vue property:
The class
attribute is assigned depending on the 'isImportant' property, if it is 'true' or 'false':
<div v-bind:class="{ myClass: isImportant }">
The class is set conditionally to change the background color
</div>
Try it Yourself »
v-bind
The shorthand for 'v-bind:
' is simply ':
'.
Here we just write ':
' instead of 'v-bind:
':
<div
:class="{ impClass: isImportant }">
The class is set conditionally to change the background color
</div>
Try it Yourself »
We will continue to use v-bind:
syntax in this tutorial to avoid confusion.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!