Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮   
     ❯   

Vue Tutorial

Vue HOMEVue IntroVue DirectivesVue v-bindVue v-ifVue v-showVue v-forVue EventsVue v-onVue MethodsVue Event ModifiersVue FormsVue v-modelVue CSS BindingVue Computed PropertiesVue WatchersVue Templates

Scaling Up

Vue Why, How and SetupVue First SFC PageVue ComponentsVue PropsVue v-for ComponentsVue $emit()Vue Fallthrough AttributesVue Scoped StylingVue Local ComponentsVue SlotsVue v-slotVue Scoped SlotsVue Dynamic ComponentsVue TeleportVue HTTP RequestVue Template RefsVue Lifecycle HooksVue Provide/InjectVue RoutingVue Form InputsVue AnimationsVue Animations with v-forVue BuildVue Composition API

Vue Reference

Vue Built-in AttributesVue Built-in ComponentsVue Built-in ElementsVue Component InstanceVue DirectivesVue Instance OptionsVue Lifecycle Hooks

Vue Examples

Vue ExamplesVue ExercisesVue QuizVue SyllabusVue Study PlanVue ServerVue Certificate

Vue v-bind Directive


Example

Using thev-bind directive to change the background color of a<div> element.

<template>  <h2>Example v-bind Directive</h2>  <p>The v-bind directive connects the style attribute of the DIV element to the 'colorVal' data property.</p>  <div v-bind:style="{ backgroundColor: colorVal }">DIV element</div>  <p>Use the input type="color" box below to change the color.</p>  <p><input type="color" v-model="colorVal"> <pre>colorVal: '{{ colorVal }}'</pre></p></template>
Run Example »

See more examples below.


Definition and Usage

Thev-bind directive is used to bind an HTML attribute to a property in the Vue instance (Example above), or to pass props (Example 1 below).

If we change a Vue instance property, and that property is bound to an HTML attribute withv-bind, the HTML element will be updated with the new attribute value automatically thanks to Vue's reactivity system.

The shorthand for 'v-bind:' is simply ':', or '.' when used with the.prop modifier.

These modifiers can be used with thev-bind directive, but are often not needed:

ModifierDetails
.camelTransforms an attribute name from kebab-case to camelCase. This is not need when using a build step, or when using String templates.
.propForces a binding to be set as a DOM property. Unless working with custom elements, Vue will find out if the key provided withv-bind is a DOM property or an attribute to the element, and bind the key appropriately.
.attrForces a binding to be set as a DOM attribute. Unless working with custom elements, Vue will find out if the key provided withv-bind is a DOM property or an attribute to the element, and bind the key appropriately.

More Examples

Example 1

Usingv-bind to send the 'img' prop, with data type boolean (true/false).

<template>  <h2>Example v-bind Directive</h2>  <p>Two props are sent to the component. We must use v-bind for the prop with 'boolean' data type.</p>  <button v-on:click="this.img = !this.img">Toggle 'img' prop value</button> {{ img }}  <info-box     turtle-text="Turtles can hold their breath for a long time."    v-bind:turtle-img="img"  /></template><script>export default {  data() {    return {      img: true    }  }}</script>
Run Example »

Example 2

Using the 'v-bind:' shorthand ':'.

<template>  <h2>Example v-bind Directive</h2>  <p>Two props are sent to the component. We must use v-bind for the prop with 'boolean' data type.</p>  <button v-on:click="this.img = !this.img">Toggle 'img' prop value</button> {{ img }}  <info-box     turtle-text="Turtles can hold their breath for a long time."    :turtle-img="img"  /></template><script>export default {  data() {    return {      img: true    }  }}</script>
Run Example »

Example 3

Using the.prop modifier to change theindeterminate DOM property of the checkbox.

<template>  <p>Using the '.prop' modifier to toggle the 'indeterminate' appearance of the checkbox:</p>  <button v-on:click="indetVal = !indetVal">Toggle</button>  <p>    <input type="checkbox" v-bind:indeterminate.prop="indetVal"> Checkbox  </p></template><script>export default {  data() {    return {      indetVal: false    };  }};</script><style>input {  margin: 20px;  scale: 2;}</style>
Run Example »

Example 4

Using the.prop modifier shorthand, and thev-bindshorthand, so that 'v-bind:indeterminate.prop' becomes '.indeterminate'.

<template>  <p>Using the '.prop' shorthand so that 'v-bind:indeterminate.prop' becomes '.indeterminate':</p>  <button v-on:click="indetVal = !indetVal">Toggle</button>  <p>    <input type="checkbox" .indeterminate="indetVal"> Checkbox  </p></template><script>export default {  data() {    return {      indetVal: false    };  }};</script><style scoped>input {  margin: 10px;  scale: 2;}</style>
Run Example »

Related Pages

Vue Tutorial:Vue CSS Binding

Vue Tutorial:Vue v-bind Directive

Vue Tutorial:Vue Props



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp