Vue.js uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the Vue instance's data. The templates are compiled into Virtual DOM render functions.
The most basic form of template is text interpolation using the "Mustache" syntax (double curly braces):
<div>Message: {{ msg }}</div>
The mustache tag will be replaced with the value of the
msg property from the
data object. It will also be updated whenever the value of the "msg" property changes.
- Example, when click on a button it sets a value o a data Vue property (here "str"), changing the text displayed into a Div:
<div id='app'>
<div>{{ str }}</div>
<button @click='str = "Love the Life"'>Click</button>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {str: 'Be Yourself.'}
})
</script>
HTML tags in template
The double mustaches interprets the data as plain text. To output real HTML, you will need to use the v-html
directive.
Example:
<div id='app'>
<p>Using mustaches: {{ str_html }}</p>
<p>Using v-html directive: <span v-html="str_html"></span></p>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {str_html: '<b style="color:#00f">Be Yourself.</b>'}
})
</script>
Mustaches cannot be used inside HTML attributes.
Using JavaScript Expressions in Vue Template
Vue.js supports JavaScript expressions inside all data bindings (one single expression):
{{ number + 1 }}
{{ ok ? 'YES' : 'NO' }}
{{ message.split('').reverse().join('') }}
<div v-bind:id="'list-' + id"></div>
- Example, shows the number of clicks on a button:
<div id='app'>
<div>Number of licks: {{ nr}}</div>
<button @click='nr +=1'>Click</button>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {nr: 0}
})
</script>
Another example, display in reverse order the characters added into an input text field:
<div id='app'>
<p>{{ msg.split('').reverse().join('') }}</p>
Add some text in this input:<br>
<input v-model='msg'>
</div>
<script>
var vue_ob = new Vue({
el: '#app',
data: {msg: 'Hello Vue!'}
})
</script>
Daily Test with Code Example
HTML
CSS
JavaScript
PHP-MySQL
Which tag create a highlighted bolded text?
<q> <strong> <em><p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;#id {
font-weight: 800;
}
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;$vname = 8;
echo $vname;