YouTip LogoYouTip

Vue Template Syntax

# Vue.js Template Syntax Vue.js uses an HTML-based template syntax that allows developers to declaratively bind the DOM to the underlying Vue instance's data. The core of Vue.js is a system that allows you to declaratively render data into the DOM using a concise template syntax. Combined with a reactive system, when the application state changes, Vue can intelligently calculate the minimal cost of re-rendering components and apply it to DOM operations. * * * ## Interpolation ### Text The most common form of data binding is text interpolation using {{...}} (double curly braces): ## Text Interpolation

{{ message }}

[Try it Β»](#) ### Html The v-html directive is used to output raw HTML: ## v-html Directive
new Vue({ el: '#app', data: { message: '

Tutorial

' } }) [Try it Β»](#) ### Attributes HTML attribute values should use the v-bind directive. The following example checks the value of `use`. If it's `true`, the `class1` style is applied; otherwise, it is not: ## v-bind Directive


v-bind:class directive
new Vue({ el: '#app', data:{ use: false } }); [Try it Β»](#) ### Expressions Vue.js provides complete support for JavaScript expressions. ## JavaScript Expressions
{{5+5}}
{{ ok ? 'YES' : 'NO' }}
{{ message.split('').reverse().join('') }}
Tutorial
new Vue({ el: '#app', data: { ok: true, message: 'TUTORIAL', id : 1 } }) [Try it Β»](#) * * * ## Directives Directives are special attributes with the `v-` prefix. Directives apply certain behavior to the DOM when the value of their expression changes. For example: ## Example

Now you see me

new Vue({ el: '#app', data: { seen: true } }) [Try it Β»](#) Here, the `v-if` directive will conditionally insert the `

` element based on the truthiness of the expression `seen` (true or false). ### Parameters Parameters are indicated after a colon following the directive. For example, the `v-bind` directive is used to reactively update HTML attributes: ## Example

new Vue({ el: '#app', data: { url: '' } }) [Try it Β»](#) Here, `href` is the parameter, telling the `v-bind` directive to bind the element's `href` attribute to the value of the expression `url`. Another example is the `v-on` directive, which is used to listen to DOM events: Here, the parameter is the name of the event to listen for. ### Modifiers Modifiers are special suffixes indicated by a dot `.`, used to specify that a directive should be bound in a special way. For example, the `.prevent` modifier tells the `v-on` directive to call `event.preventDefault()` for the triggered event: * * * ## User Input In an input box, we can use the `v-model` directive to achieve two-way data binding: ## Two-way Data Binding

{{ message }}

new Vue({ el: '#app', data: { message: 'Tutorial!' } }) [Try it Β»](#) The `v-model` directive creates two-way data binding on form controls like ``, ``, `