Vue.js 例項
本章節為大家介紹幾個 Vue.js 例項,通過例項練習來鞏固學到的知識點。
導航選單例項
導航選單
建立一個簡單的導航選單:
<div id="main">
<!-- 啟用的選單樣式為 active 類 -->
<!-- 為了阻止連結在點選時跳轉,我們使用了 "prevent" 修飾符 (preventDefault 的簡稱)。 -->
<nav v-bind:class="active" v-on:click.prevent>
<!-- 當選單上的連結被點選時,我們呼叫了 makeActive 方法, 該方法在 Vue 例項中建立。 -->
<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>
<!-- 以下 "active" 變數會根據當前選中的值來自動變換 -->
<p>您選擇了 <b>{{active}} 選單</b></p>
</div>
<script>
// 建立一個新的 Vue 例項
var demo = new Vue({
// DOM 元素,掛載檢視模型
el: '#main',
// 定義屬性,並設定初始值
data: {
active: 'home'
},
// 點選選單使用的函式
methods: {
makeActive: function(item){
// 模型改變,檢視會自動更新
this.active = item;
}
}
});
</script>
嘗試一下 ?
編輯文字例項
文字編輯
點選指定文字編輯內容:
<!-- v-cloak 隱藏未編譯的變數,直到 Vue 例項準備就緒。 -->
<!-- 元素點選後 hideTooltp() 方法被呼叫 -->
<div id="main" v-cloak v-on:click="hideTooltip">
<!-- 這是一個提示框
v-on:click.stop 是一個點選事件處理器,stop 修飾符用於阻止事件傳遞
v-if 用來判斷 show_tooltip 為 true 時才顯示 -->
<div class="tooltip" v-on:click.stop v-if="show_tooltip">
<!-- v-model 綁定了文字域的內容
在文字域內容改變時,對應的變數也會實時改變 -->
<input type="text" v-model="text_content" />
</div>
<!-- 點選後呼叫 "toggleTooltip" 方法並阻止事件傳遞 -->
<!-- "text_content" 變數根據文字域內容的變化而變化 -->
<p v-on:click.stop="toggleTooltip">{{text_content}}</p>
</div>
<script>
var demo = new Vue({
el: '#main',
data: {
show_tooltip: false,
text_content: '點我,並編輯內容。'
},
methods: {
hideTooltip: function(){
// 在模型改變時,檢視也會自動更新
this.show_tooltip = false;
},
toggleTooltip: function(){
this.show_tooltip = !this.show_tooltip;
}
}
})
</script>
嘗試一下 ?
訂單列表例項
訂單列表
建立一個訂單列表,並計算總價:
<form id="main" v-cloak>
<h1>Services</h1>
<ul>
<!-- 迴圈輸出 services 陣列, 設定選項點選後的樣式 -->
<li v-for="service in services" v-on:click="toggleActive(service)" v-bind:class="{ 'active': service.active}">
<!-- 顯示訂單中的服務名,價格
Vue.js 定義了貨幣過濾器,用於格式化價格 -->
{{service.name}} <span>{{service.price | currency}}</span>
</li>
</ul>
<div class="total">
<!-- 計算所有服務的價格,並格式化貨幣 -->
Total: <span>{{total() | currency}}</span>
</div>
</form>
<script>
// 自定義過濾器 "currency"
Vue.filter('currency', function (value) {
return '$' + value.toFixed(2);
});
var demo = new Vue({
el: '#main',
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>
嘗試一下 ?
搜尋頁面例項
搜尋頁面
在輸入框輸入搜尋內容,列表顯示配置的列表:
<form id="main" v-cloak>
<div class="bar">
<!-- searchString 模型與文字域建立繫結 -->
<input type="text" v-model="searchString" placeholder="輸入搜尋內容" />
</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: "",
// 資料模型,實際環境你可以根據 Ajax 來獲取
articles: [
{
"title": "What You Need To Know About CSS Variables",
"url": "https://www.itread01.com/css/css-tutorial.html",
"image": "https://static.itread01.com/images/icon/css.png"
},
{
"title": "Freebie: 4 Great Looking Pricing Tables",
"url": "https://www.itread01.com/html/html-tutorial.html",
"image": "https://static.itread01.com/images/icon/html.png"
},
{
"title": "20 Interesting JavaScript and CSS Libraries for February 2016",
"url": "https://www.itread01.com/css3/css3-tutorial.html",
"image": "https://static.itread01.com/images/icon/css3.png"
},
{
"title": "Quick Tip: The Easiest Way To Make Responsive Headers",
"url": "https://www.itread01.com/css3/css3-tutorial.html",
"image": "https://static.itread01.com/images/icon/css3.png"
},
{
"title": "Learn SQL In 20 Minutes",
"url": "https://www.itread01.com/sql/sql-tutorial.html",
"image": "https://static.itread01.com/images/icon/sql.png"
},
{
"title": "Creating Your First Desktop App With HTML, JS and Electron",
"url": "https://www.itread01.com/js/js-tutorial.html",
"image": "https://static.itread01.com/images/icon/html.png"
}
]
},
computed: {
// 計算屬性,匹配搜尋
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 articles_array;;
}
}
});
</script>
嘗試一下 ?
切換不同佈局例項
切換不同佈局
點選右上角的按鈕來切換不同頁面佈局:
<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>
<!-- 我們設定了兩套佈局頁面。使用哪套依賴於 "layout" 繫結 -->
<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>
<script>
var demo = new Vue({
el: '#main',
data: {
// 檢視模型,可能的值是 "grid" 或 "list"。
layout: 'grid',
articles: [{
"title": "HTML 教程",
"url": "https://www.itread01.com/html/html-tutorial.html",
"image": {
"large": "https://static.itread01.com/images/mix/htmlbig.png",
"small": "https://static.itread01.com/images/icon/html.png"
}
},
{
"title": "CSS 教程",
"url": "https://www.itread01.com/css/css-tutorial.html",
"image": {
"large": "https://static.itread01.com/images/mix/cssbig.png",
"small": "https://static.itread01.com/images/icon/css.png"
}
},
{
"title": "JS 教程",
"url": "https://www.itread01.com/js/js-tutorial.html",
"image": {
"large": "https://static.itread01.com/images/mix/jsbig.jpeg",
"small": "https://static.itread01.com/images/icon/js.png"
}
},
{
"title": "SQL 教程",
"url": "https://www.itread01.com/sql/sql-tutorial.html",
"image": {
"large": "https://static.itread01.com/images/mix/sqlbig.png",
"small": "https://static.itread01.com/images/icon/sql.png"
}
},
{
"title": "Ajax 教程",
"url": "https://www.itread01.com/ajax/ajax-tutorial.html",
"image": {
"large": "https://static.itread01.com/images/mix/ajaxbig.png",
"small": "https://static.itread01.com/images/icon/ajax.png"
}
},
{
"title": "Python 教程",
"url": "https://www.itread01.com/pyhton/pyhton-tutorial.html",
"image": {
"large": "https://static.itread01.com/images/mix/pythonbig.png",
"small": "https://static.itread01.com/images/icon/python.png"
}
}]
}
});
</script>
嘗試一下 ?