Vue.js Instances
This chapter introduces several Vue.js examples to reinforce the knowledge points learned through hands-on practice.
Navigation Menu Example
Navigation Menu
Create a simple navigation menu:
<div id="main">
<nav v-bind:class="active" v-on:click>.
prevent
<a href="" class="home" v-on:click="makeActive('home')" Home a>
<a href="" class="projects" v-on:click="makeActive('projects')" Projects a>
<a href="" class="services" v-on:click="makeActive('services')" Services a>
<a href="" class="contact" v-on:click="makeActive('contact')" Contact a nav>
<p>You selected
<
b
{{active}} menu
b
p
div
<
script
// create a new Vue instance var demo = new Vue({ // DOM element, mount the view model el: '#main', // define properties and set initial values data: { active: 'home' }, // the function used when clicking the menu methods: { makeActive: function(item){ // when the model changes, the view updates automatically this.active = item; } } });
script
Try it »
Edit Text Example
Text Editing
Click the specified text to edit the content:
<div id="main" v-cloak v-on:click="hideTooltip">
<div class="tooltip" v-on:click>.
stop
v-if
=
"
show_tooltip
"
<input type="text" v-model="text_content" />div>
<p v-on:click>.
stop
=
"
toggleTooltip
"
{{text_content}}
p
div
<
script
var demo = new Vue({ el: '#main', data: { show_tooltip: false, text_content: 'Click me and edit the content.' }, methods: { hideTooltip: function(){ // when the model changes, the view also updates automatically this.show_tooltip = false; }, toggleTooltip: function(){ this.show_tooltip = !this.show_tooltip; } } })
script
Try it »
Order List Example
Order List
Create an order list and calculate the total price:
<form id="main" v-cloak>
<h1 Services h1>
<ul>
<li v-for=">service in services
"
v-on:click
=
"
toggleActive(service)
"
v-bind:class
=
"
{ 'active': service.active}
"
{{service.name}}
<
span
{{service.price | currency}}
span
li
ul
<div class="total">Total:
<
span
{{total() | currency}}
span
div
form
<
script
// custom filter "currency" Vue.filter('currency', function (value) { return '$' + value.toFixed(2); }); var demo = new Vue({ el: '#main', data: { // define model properties // the view will loop over the array data services: [ { name: 'Web Development', price: 300, active:true },{ name: 'Design', price: 400, active:false },{ name: 'Integration', price: 250, active:false },{ name: 'Training', price: 220, active:false } ] }, methods: { toggleActive: function(s){ s.active = !s.active; }, total: function(){ var total = 0; this.services.forEach(function(s){ if (s.active){ total+= s.price; } }); return total; } } });
script
Try it »
Search Page Example
Search Page
Enter search content in the input box, and the list displays the configured list:
<form id="main" v-cloak>
<div class="bar">
<input type="text" v-model="searchString" placeholder=">Enter the search text
"
/>
div
<ul>
<li v-for=">article in filteredArticles
"
<a v-bind:href="article.url">
<img v-bind:src="article.image" />a>
<p{{article.title}}p li ul form>
<script>var demo = new Vue({ el: '#main', data: { searchString: "", // data model, in a real environment you can fetch it via Ajax articles: [ { "title": "What You Need To Know About CSS Variables", "url": "https://www.runoob.com/css/css-tutorial.html", "image": "https://static.jyshare.com/images/icon/css.png" }, { "title": "Freebie: 4 Great Looking Pricing Tables", "url": "https://www.runoob.com/html/html-tutorial.html", "image": "https://static.jyshare.com/images/icon/html.png" }, { "title": "20 Interesting JavaScript and CSS Libraries for February 2016", "url": "https://www.runoob.com/css3/css3-tutorial.html", "image": "https://static.jyshare.com/images/icon/css3.png" }, { "title": "Quick Tip: The Easiest Way To Make Responsive Headers", "url": "https://www.runoob.com/css3/css3-tutorial.html", "image": "https://static.jyshare.com/images/icon/css3.png" }, { "title": "Learn SQL In 20 Minutes", "url": "https://www.runoob.com/sql/sql-tutorial.html", "image": "https://static.jyshare.com/images/icon/sql.png" }, { "title": "Creating Your First Desktop App With HTML, JS and Electron", "url": "https://www.runoob.com/js/js-tutorial.html", "image": "https://static.jyshare.com/images/icon/html.png" } ] }, computed: { // computed property, matches the search filteredArticles: function () { var articles_array = this.articles, searchString = this.searchString; if(!searchString){ return articles_array; } searchString = searchString.trim().toLowerCase(); articles_array = articles_array.filter(function(item){ if(item.title.toLowerCase().indexOf(searchString) !== -1){ return item; } }) // return the filtered data return articles_array;; } } });
script
Try it »
Switch Different Layouts Example
Switch Different Layouts
Click the button in the top-right corner to switch between different page layouts:
<form id="main" v-cloak>
<div class="bar">
<a class="list-icon" v-bind:class=">{ 'active': layout == 'list'}
"
v-on:click
=
"
layout = 'list'
"
a
<a class="grid-icon" v-bind:class=">{ 'active': layout == 'grid'}
"
v-on:click
=
"
layout = 'grid'
"
a
div
<ul v-if=">layout == 'grid'
"
class
=
"
grid
"
<li v-for=">a in articles
"
<a v-bind:href="a.url" target="_blank" rel=">noopener noreferrer
"
<img v-bind:src="a.image.large" />a li ul>
<ul v-if=">layout == 'list'
"
class
=
"
list
"
<li v-for=">a in articles
"
<a v-bind:href="a.url" target="_blank" rel=">noopener noreferrer
"
<
img
v-bind:src
=
"
a.image.small
"
/>
a
<
p
{{a.title}}
p
li
ul
form
<
scriptvar demo = new Vue({ el: '#main', data: { // view model, possible values are "grid" or "list". layout: 'grid', articles: [{ "title": "HTML Tutorial", "url": "https://www.runoob.com/html/html-tutorial.html", "image": { "large": "https://static.jyshare.com/images/mix/htmlbig.png", "small": "https://static.jyshare.com/images/icon/html.png" } }, { "title": "CSS Tutorial", "url": "https://www.runoob.com/css/css-tutorial.html", "image": { "large": "https://static.jyshare.com/images/mix/cssbig.png", "small": "https://static.jyshare.com/images/icon/css.png" } }, { "title": "JS Tutorial", "url": "https://www.runoob.com/js/js-tutorial.html", "image": { "large": "https://static.jyshare.com/images/mix/jsbig.jpeg", "small": "https://static.jyshare.com/images/icon/js.png" } }, { "title": "SQL Tutorial", "url": "https://www.runoob.com/sql/sql-tutorial.html", "image": { "large": "https://static.jyshare.com/images/mix/sqlbig.png", "small": "https://static.jyshare.com/images/icon/sql.png" } }, { "title": "Ajax Tutorial", "url": "https://www.runoob.com/ajax/ajax-tutorial.html", "image": { "large": "https://static.jyshare.com/images/mix/ajaxbig.png", "small": "https://static.jyshare.com/images/icon/ajax.png" } }, { "title": "Python Tutorial", "url": "https://www.runoob.com/pyhton/pyhton-tutorial.html", "image": { "large": "https://static.jyshare.com/images/mix/pythonbig.png", "small": "https://static.jyshare.com/images/icon/python.png" } }] } });
script
Try it »