Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue.js Template Syntax

Vue.js Template Syntax

Vue.js uses an HTML-based template syntax that allows developers to declaratively bind the DOM to the data of the underlying Vue instance.

At the core of Vue.js is a system that lets you declaratively render data into the DOM with a concise template syntax.

Combined with the reactivity system, when the application state changes, Vue intelligently computes the minimal cost to re-render components and applies it to DOM operations.


Interpolation

Text

The most common form of data binding is text interpolation using {{...}} (double curly braces):

Text Interpolation

<div id="app">
<p>

{{ message }}

p

div

Try it »

Html

Use the v-html directive to output HTML code:

v-html directive

<div id="app">
<div v-html="message" div div>
<script>

new Vue({ el: '#app', data: { message: '

<

h1

Runoob Tutorial

h1

' } })

script

Try it »

property

Values in HTML attributes should use the v-bind directive.

The following example checks the value of use; if true, it uses the class1 class style, otherwise it does not:

v-bind directive

<div id="app">
<label for="r1">

Change color

label

<input type="checkbox" v-model="use" id="r1">
<br>
<br>
<div v-bind:class=">

{'class1': use}

"

v-bind:class directive

div

div

<

script

new Vue({ el: '#app', data:{ use: false } });

script

Try it »

Expression

Vue.js provides full JavaScript expression support.

JavaScript Expressions

<div id="app" {{5+5}}>
<br>

{{ ok ? 'YES' : 'NO' }}

<

br

{{ message.split('').reverse().join('') }}

<div v-bind:id=">

'list-' + id

"

Runoob Tutorial

div

div

<

script

new Vue({ el: '#app', data: { ok: true, message: 'RUNOOB', id : 1 } })

script

Try it »


Instruction

Directives are special attributes prefixed with v-.

Directives are used to apply certain behaviors to the DOM when the value of the expression changes. Example:

Example

<div id="app">
<p v-if="seen">

Now you see me

p

div

<

script

new Vue({ el: '#app', data: { seen: true } })

script

Try it »

Here, the v-if directive will decide whether to insert the p element based on the value of the expression seen (true or false).

Parameters

Arguments are specified after the directive with a colon. For example, the v-bind directive is used to responsively update HTML attributes:

Example

<div id="app">
<pre>
<a v-bind:href="url">

Runoob Tutorial

a

pre

div

<

script

new Vue({ el: '#app', data: { url: 'http://www.runoob.com' } })

script

Try it »

Here href is the argument, 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, used to listen for DOM events:





Here the argument is the name of the event being listened to.

Modifiers

Modifiers are special suffixes indicated by a period (.). They indicate that a directive should be bound in a special way. For example,.prevent modifier tells the v-on directive to call event.preventDefault():


User input

In an input box we can use the v-model directive to achieve two-way data binding:

Two-Way Data Binding

<div id="app">
<p>

{{ message }}

p

<input v-model="message" div>
<script>

new Vue({ el: '#app', data: { message: 'Runoob!' } })

script

Try it »

The v-model directive is used to create two-way data binding on form controls such as input, select, textarea, checkbox, and radio, automatically updating the bound element's value based on the form's value.

For button events we can use v-on to listen for events and respond to user input.

The following example reverses a string after the user clicks the button:

Reverse String

<div id="app">
<p>

{{ message }}

p

<button v-on:click="reverseMessage">

Reverse String

button

div

<

script

new Vue({ el: '#app', data: { message: 'Runoob!' }, methods: { reverseMessage: function () { this.message = this.message.split('').reverse().join('') } } })

script

Try it »


Filter

Vue.js allows you to define custom filters, used for common text formatting. Indicated by the "pipe" symbol, the format is as follows:





{{ message | capitalize }}





The filter function receives the value of the expression as its first argument.

The following example capitalizes the first letter of the input string:

Example

<div id="app">

{{ message | capitalize }}

div

<

script

new Vue({ el: '#app', data: { message: 'runoob' }, filters: { capitalize: function (value) { if (!value) return '' value = value.toString() return value.charAt(0).toUpperCase() + value.slice(1) } } })

script

Try it »

Filters can be chained:

{{ message | filterA | filterB }}

Filters are JavaScript functions, so they can accept arguments:

{{ message | filterA('arg1', arg2) }}

Here, message is the first argument, the string 'arg1' will be passed to the filter as the second argument, and the value of the arg2 expression will be evaluated and passed to the filter as the third argument.


Shorthands

v-bind shorthand

Vue.js provides special shorthands for the two most commonly used directives:











v-on shorthand