vue學(xué)習(xí)筆記-寫一個(gè)todolist

相信很多前端小盆友在剛剛過去的2016年都看到過這樣一片文章
在 2016 年學(xué) JavaScript 是一種什么樣的體驗(yàn)?

圖片來源于網(wǎng)絡(luò)

確實(shí)在剛剛過去的一年前端方面涌現(xiàn)出許多新鮮的技術(shù)或者許多技術(shù)在社區(qū)取得了巨大的發(fā)展進(jìn)步,社區(qū)資料顯示出2016年最流行的前端框架是Vue.JS,因?yàn)楣卷?xiàng)目沒有涉及所以也沒有深入了解,文檔讀了許多遍還只停留在前幾節(jié)(慚愧狀-_-||)

心塞塞

最近趁年后回來公司沒什么任務(wù)好好的讀了一下文檔及許多前輩的博客,決定分享一下自己的學(xué)習(xí)心得然后寫一個(gè)todolist算是對自己最近學(xué)習(xí)的整理和總結(jié),也希望本文能幫助其他剛開始學(xué)vue的小伙伴對它有更好的理解,下面開始吧

不廢話
vue的相關(guān)資料及優(yōu)點(diǎn)我就不總結(jié)了,網(wǎng)上一堆堆bulabula...

一.步驟總結(jié)

用vue寫項(xiàng)目大概可以總結(jié)為三個(gè)步驟

1.注冊組件
2.應(yīng)用組件
3.實(shí)例化

下面我們來舉個(gè)栗子


舉個(gè)栗子
1.注冊組件(js)
<script>
    //注冊組件,格式為Vue.component("自定義標(biāo)簽",{相關(guān)模板,數(shù)據(jù),函數(shù)等屬性})
    Vue.component('my-component', {
        template: '<button>{{ message }}</button>',
        //注冊組件是的data必須是函數(shù),與實(shí)例化時(shí)使用的不同
        data: function(){
            return {
                message:"我是一個(gè)可愛的組件"
            }
        }
    })
</script>
2.應(yīng)用組件(html)
<div id="vm">
    <my-component></my-component> 
</div>
3.實(shí)例化(js)
new Vue({
        el:"#vm"
 })

這樣一個(gè)最基本的模板就好了
效果如下

簡易組件
如果想把功能及效果寫的更炫酷大家可以隨著技術(shù)的成長和積累后續(xù)了解vue的相關(guān)API及語法,如props,watcher,methods,computed.......下面來做一個(gè)todolist的Demo

二.實(shí)現(xiàn)一個(gè)todolist

代碼部分
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--樣式庫-->
    <link rel="stylesheet" >
    <!--vue.js庫-->
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <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>
</head>
<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">添加任務(wù)事項(xiàng)</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">
                        任務(wù)清單:
                    </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">任務(wù)計(jì)劃:</div>
                <div class="panel-body">
                    請?jiān)谝恢軆?nèi)完成這些計(jì)劃!
                </div>
                <div class="panel-footer text-right">
                    <button class="btn btn-info btn-sm" @click="clearItem">清空任務(wù)計(jì)劃</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    //該網(wǎng)站的localStorage的鍵值,用于存放數(shù)據(jù)
    var todoList = 'todolist';
    //對localStorage的封裝
    var lsp = (function () {
        return ({
            add: function (dataval) {
                //添加數(shù)據(jù),鍵為todolist
                localStorage.setItem(todoList, JSON.stringify(dataval));
            },
            get: function () {
                //讀取鍵為todolist的數(shù)據(jù)
                return JSON.parse(localStorage.getItem(todoList));
            },
            remove: function () {
                //移除該站點(diǎn)下鍵為todolist的數(shù)據(jù)
                localStorage.removeItem(todoList);
            },
            clear: function () {
                //清空該站點(diǎn)下的所有l(wèi)ocalStorage的數(shù)據(jù)
                localStorage.clear();
            }
        });
    })();
    var app = new Vue({
        el: '#app',
        data: {
            title: '任務(wù)清單demo',
            items: lsp.get() || [],//讀取數(shù)據(jù)。如果沒有數(shù)據(jù)賦值為數(shù)組[]
            newitem: '' //要添加的數(shù)據(jù)
        },
        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>
todolist.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 昨夜星辰昨夜風(fēng),本應(yīng)好景良辰,驚悉君之困頓憂擾,如鯁在喉,署文以記之。 常常在白天彷徨迷失,常常在深夜里咀嚼舔舐。...
    你虧欠我一段小時(shí)光閱讀 579評論 0 0
  • 你繼承了愛,也繼承了愛的方式 文:Stanley 1 一桌子人在吃飯,酒過三巡,菜過五味,其中一個(gè)哥們兒帶點(diǎn)醉意,...
    楊慶瑞閱讀 930評論 6 10
  • 不想一直過現(xiàn)在一塵不變,沒有一點(diǎn)激情,沒有一點(diǎn)波瀾,有點(diǎn)虛度時(shí)光,毫無意義的生活。 天天做重復(fù)的工作,焊線焊線焊線...
    她愛她閱讀 197評論 0 0
  • 曉光暗影冬陽晚,霜意重、錦衾清暖。蕭索遠(yuǎn)山間,殘霧堆云卷。 病中猶夢家鄉(xiāng)盼,默無語、空留悔嘆。筆墨砌銷魂,客老人悲咽。
    詩酒慰年華的夏川閱讀 248評論 3 1

友情鏈接更多精彩內(nèi)容