top
Loading...
Vue.js 實例

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: { // 定義模型屬性 the model properties. The view will loop // 視圖將循環輸出數組的數據 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.sharebody.com/css/css-tutorial.html", "image": "https://static./images/icon/css.png" }, { "title": "Freebie: 4 Great Looking Pricing Tables", "url": "https://www.sharebody.com/html/html-tutorial.html", "image": "https://static./images/icon/html.png" }, { "title": "20 Interesting JavaScript and CSS Libraries for February 2016", "url": "https://www.sharebody.com/css3/css3-tutorial.html", "image": "https://static./images/icon/css3.png" }, { "title": "Quick Tip: The Easiest Way To Make Responsive Headers", "url": "https://www.sharebody.com/css3/css3-tutorial.html", "image": "https://static./images/icon/css3.png" }, { "title": "Learn SQL In 20 Minutes", "url": "https://www.sharebody.com/sql/sql-tutorial.html", "image": "https://static./images/icon/sql.png" }, { "title": "Creating Your First Desktop App With HTML, JS and Electron", "url": "https://www.sharebody.com/js/js-tutorial.html", "image": "https://static./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"><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"><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.sharebody.com/html/html-tutorial.html", "image": { "large": "https://static./images/mix/htmlbig.png", "small": "https://static./images/icon/html.png" } }, { "title": "CSS 教程", "url": "https://www.sharebody.com/css/css-tutorial.html", "image": { "large": "https://static./images/mix/cssbig.png", "small": "https://static./images/icon/css.png" } }, { "title": "JS 教程", "url": "https://www.sharebody.com/js/js-tutorial.html", "image": { "large": "https://static./images/mix/jsbig.jpeg", "small": "https://static./images/icon/js.png" } }, { "title": "SQL 教程", "url": "https://www.sharebody.com/sql/sql-tutorial.html", "image": { "large": "https://static./images/mix/sqlbig.png", "small": "https://static./images/icon/sql.png" } }, { "title": "Ajax 教程", "url": "https://www.sharebody.com/ajax/ajax-tutorial.html", "image": { "large": "https://static./images/mix/ajaxbig.png", "small": "https://static./images/icon/ajax.png" } }, { "title": "Python 教程", "url": "https://www.sharebody.com/pyhton/pyhton-tutorial.html", "image": { "large": "https://static./images/mix/pythonbig.png", "small": "https://static./images/icon/python.png" } }] } }); </script>

嘗試一下 »
北斗有巢氏 有巢氏北斗