DIV CSS 佈局教程網

用 Vue 寫一個 Todolist 單頁應用(不用脚手架)
編輯:關於JavaScript     

網上的 Vue Todolist 教程大多基於 vue-cli 脚手架,官網文檔對新手也不太友好。這裡用最傳統的方式——一個 HTML 文件——把 TodoList 寫出來,方便從零理解 Vue 的核心用法。

一、Vue 是什麼

Vue.js(讀音 /vjuː/,近似 view)是一套構建用戶界面的漸進式框架。簡單說它可以當作模板引擎來用:做過后端的同學應該很熟悉,以前靠服務器端渲染 DOM,現在把渲染搬到浏覽器端,Vue 拿到數據後生成 DOM。當然它不僅能做這個——數據驅動、雙向綁定帶來了更好的開發效率與用戶體驗,也讓項目更易維護。

二、準備工作

提前引入 vue.js 與 bootstrap。由於沒有採用單文件開發,所以整個應用只有一個 html 文件。文件可以通過 CDN 引入,也可以下載到本地。

本 demo 使用 localStorage 存放數據,因此必須通過 web 服務器訪問(直接雙擊打開靜態文件是無法讀寫 localStorage 的)。用 WebStorm 這類 IDE 開發會方便很多。當然,實際項目裡這些數據完全可以換成從數據庫讀取。

三、完整代碼

 <!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>vue版todolist</title>
 <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
 <script src="src/vue.js"></script>
 </head>
 <style>
 .isFinish { background-color: #d58512 !important; }
 .itemcount {
 display: block; width: 20px; height: 20px; text-align: center;
 line-height: 20px; border-radius: 10px; float: left; background-color: #d9edf7;
 }
 </style>
 <body>
 <div class="container text-center" id="app">
 <h2>{{title}}</h2>
 <div class="row">
 <div class="col-md-7">
 <form class="form-horizontal" role="form">
 <div class="form-group">
 <label for="toitem">添加任務事項</label>
 <input class="form-control" type="text" id="toitem" v-model="newitem" @keyup.enter="addItem()">
 </div>
 <div class="list-group text-left form-group" style="margin-top: 2em;">
 <a href="#" class="list-group-item active text-left">
 任務清單:
 </a>
 <a href="#" v-for="item in items" class="list-group-item" v-on:click="toogleFinsih(item)">
 <span class="itemcount">{{item.id}}</span>
 {{item.lable}}
 <span class="badge" v-bind:class="{isFinish:item.isFinish}">√</span>
 </a>
 </div>
 </form>
 </div>
 <div class="col-md-5">
 <div class="panel panel-default">
 <div class="panel-heading">任務計劃:</div>
 <div class="panel-body">
 請在一周內完成這些計劃!
 </div>
 <div class="panel-footer text-right">
 <button class="btn btn-info btn-sm" @click="clearItem">清空任務計劃</button>
 </div>
 </div>
 </div>
 </div>
 </div>
 <script>
 //該網站的 localStorage 鍵值,用於存放數據
 var todoList = 'todolist';
 //對 localStorage 的封裝
 var lsp = (function () {
 return ({
 add: function (dataval) {
 //添加數據,鍵為 todolist
 localStorage.setItem(todoList, JSON.stringify(dataval));
 },
 get: function () {
 //讀取鍵為 todolist 的數據
 return JSON.parse(localStorage.getItem(todoList));
 },
 remove: function () {
 //移除該站點下鍵為 todolist 的數據
 localStorage.removeItem(todoList);
 },
 clear: function () {
 //清空該站點下的所有 localStorage 數據
 localStorage.clear();
 }
 });
 })();

 var app = new Vue({
 el: '#app',
 data: {
 title: '任務清單demo',
 items: lsp.get() || [],//讀取數據,如果沒有數據賦值為數組 []
 newitem: '' //要添加的數據
 },
 methods: {
 addItem: function () {
 var that = this;
 this.items.push({
 id: that.items.length + 1,
 lable: that.newitem,
 isFinish: false
 });
 lsp.add(this.items);
 this.newitem = '';
 },
 toogleFinsih: function (item) {
 item.isFinish = !item.isFinish;
 },
 clearItem: function () {
 this.items = [];
 }
 }
 })
 </script>
 </body>
 </html>

四、要點解析

雙向綁定:v-model="newitem" 讓輸入框與數據實時同步,配合 @keyup.enter="addItem()" 就能實現回車提交,不需要手寫 keydown 判斷 keyCode。

列表渲染:v-for="item in items" 遍歷數組生成節點。Vue 會自動跟蹤數據變化,push 一項之後 DOM 無需手工操作就會更新。

動態樣式:v-bind:class="{isFinish:item.isFinish}" 根據數據真假決定是否掛上 class,這裡用來標記已完成項。

localStorage 封裝:用 IIFE 把增刪查清包成一個對象,業務代碼只依賴 lsp 這個接口,日後想換成 Ajax 請求,只需替換這一層即可,這是很值得沿用的分層習慣。

五、可以繼續完善的地方

一是 clearItem 只清了 items 數組,沒有同步調用 lsp.remove(),刷新頁面數據會回來,這是個真實的 bug。

二是 id 用 items.length + 1 生成,刪除中間項後會重號,更穩妥的做法是維護一個自增的 uid。

三是可以補上單項刪除與編輯功能,並用 computed 實現「未完成數量」統計,把 Vue 的計算屬性也用起來。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved