vue -- 實例 - 記事本

新增

  • 生成列表結(jié)構(gòu)(v-for 數(shù)組)
  • 獲取用戶輸入(v-model)
  • 回車,新增數(shù)據(jù)(v-on .enter添加數(shù)據(jù))
<!-- main area -->
    <section id="todoapp">
        <!-- input -->
        <header class="header">
            <h1>NOTE</h1>
            <input v-model="inputValue" @keyup.enter="add" autofocus="autofocus" autocomplete="off" placeholder="input todo task" class="new-todo">
        </header>
        <!-- list -->
        <section class="main">
            <ul class="todo-list">
                <li class="todo" v-for="(item,index) in list">
                    <div class>
                        <span class="index">{{ index + 1 }}.</span>
                        <label>{{ item }}</label>
                        <button class="destroy"></button>
                    </div>
                </li>
            </ul>
        </section>
        <!-- 統(tǒng)計和清空 -->
        <footer class="footer">
        </footer>
     </section>
    <!-- bottom -->
    <footer class="info">
    </footer>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:'#todoapp',
            data:{
                list:[
                    'code',
                    'eat',
                    'sleep'
                ],
                inputValue:'study'
            },
            methods:{
                add:function () {
                    this.list.push(this.inputValue);
                }
            }
        })
    </script>

刪除

  • 點擊刪除指定內(nèi)容(v-on splice 索引)
  • 數(shù)據(jù)改變,和數(shù)據(jù)綁定的元素同步改變
  • 事件的自定義參數(shù)
<body>
    <!-- main area -->
    <section id="todoapp">
        <!-- input -->
        <header class="header">
            <h1>NOTE</h1>
            <input v-model="inputValue" @keyup.enter="add" autofocus="autofocus" autocomplete="off" placeholder="input todo task" class="new-todo">
        </header>
        <!-- list -->
        <section class="main">
            <ul class="todo-list">
                <li class="todo" v-for="(item,index) in list">
                    <div class>
                        <span class="index">{{ index + 1 }}.</span>
                        <label>{{ item }}</label>
                        <button class="destroy" @click="remove(index)"></button>
                    </div>
                </li>
            </ul>
        </section>
        <!-- 統(tǒng)計和清空 -->
        <footer class="footer">
        </footer>
     </section>
    <!-- bottom -->
    <footer class="info">
    </footer>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:'#todoapp',
            data:{
                list:[
                    'code',
                    'eat',
                    'sleep'
                ],
                inputValue:'study'
            },
            methods:{
                add:function () {
                    this.list.push(this.inputValue);
                },
                remove:function (index) {
                    this.list.splice(index,1);
                }
            }
        })
    </script>
</body>

統(tǒng)計和清空

  • 統(tǒng)計信息個數(shù)(v-text length)
  • 基于數(shù)據(jù)的開發(fā)方式
  • 點擊清空所有信息(v-on)
<body>
    <!-- main area -->
    <section id="todoapp">
        <!-- input -->
        <header class="header">
            <h1>NOTE</h1>
            <input v-model="inputValue" @keyup.enter="add" autofocus="autofocus" autocomplete="off" placeholder="input todo task" class="new-todo">
        </header>
        <!-- list -->
        <section class="main">
            <ul class="todo-list">
                <li class="todo" v-for="(item,index) in list">
                    <div class>
                        <span class="index">{{ index + 1 }}.</span>
                        <label>{{ item }}</label>
                        <button class="destroy" @click="remove(index)"></button>
                    </div>
                </li>
            </ul>
        </section>
        <!-- 統(tǒng)計和清空 -->
        <footer class="footer">
            <span class="todo-count">
                <strong>{{ list.length }}</strong> items left
            </span>
            <button class="clear-completed" @click="removeAll">
                clear
            </button>
        </footer>
     </section>
    <!-- bottom -->
    <footer class="info">
    </footer>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:'#todoapp',
            data:{
                list:[
                    'code',
                    'eat',
                    'sleep'
                ],
                inputValue:'study'
            },
            methods:{
                add:function () {
                    this.list.push(this.inputValue);
                },
                remove:function (index) {
                    this.list.splice(index,1);
                },
                removeAll:function () {
                   // this.list.splice(index);
                    this.list = []
                }
            }
        })
    </script>
</body>

隱藏

  • 沒有數(shù)據(jù)時 隱藏元素(v-show v-if 數(shù)組非空)
  • 列表結(jié)構(gòu)可以通過v-for指令結(jié)合數(shù)據(jù)生成
  • v-on結(jié)合事件修飾符可以對事件進行限制 比如.enter
  • v-on在綁定事件時 可以傳遞自定義參數(shù)
  • 通過v-model可以快速的設(shè)置和獲取表單元素的值
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="vue.js" type="text/javascript" charset="utf-8"></script>
    <style>
    </style>
</head>
<body>
    <!-- main area -->
    <section id="todoapp">
        <!-- input -->
        <header class="header">
            <h1>NOTE</h1>
            <input v-model="inputValue" @keyup.enter="add" autofocus="autofocus" autocomplete="off" placeholder="input todo task" class="new-todo">
        </header>
        <!-- list -->
        <section class="main">
            <ul class="todo-list">
                <li class="todo" v-for="(item,index) in list">
                    <div class>
                        <span class="index">{{ index + 1 }}.</span>
                        <label>{{ item }}</label>
                        <button class="destroy" @click="remove(index)"></button>
                    </div>
                </li>
            </ul>
        </section>
        <!-- 統(tǒng)計和清空 -->
        <footer class="footer">
            <span class="todo-count" v-if="list.length != 0">
                <strong>{{ list.length }}</strong> items left
            </span>
            <button v-show="list.length != 0" class="clear-completed" @click="removeAll">
                clear
            </button>
        </footer>
     </section>
    <!-- bottom -->
    <footer class="info">
    </footer>
    <script type="text/javascript">
        Vue.config.productionTip = false
        var app = new Vue({
            el:'#todoapp',
            data:{
                list:[
                    'code',
                    'eat',
                    'sleep'
                ],
                inputValue:'study'
            },
            methods:{
                add:function () {
                    this.list.push(this.inputValue);
                },
                remove:function (index) {
                    this.list.splice(index,1);
                },
                removeAll:function () {
                   // this.list.splice(index);
                    this.list = []
                }
            }
        })
    </script>
</body>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市犁享,隨后出現(xiàn)的幾起案子余素,更是在濱河造成了極大的恐慌,老刑警劉巖炊昆,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件桨吊,死亡現(xiàn)場離奇詭異,居然都是意外死亡凤巨,警方通過查閱死者的電腦和手機况毅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門疮胖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事柠新。” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長独榴。 經(jīng)常有香客問我,道長奕枝,這世上最難降的妖魔是什么棺榔? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮隘道,結(jié)果婚禮上症歇,老公的妹妹穿的比我還像新娘。我一直安慰自己谭梗,他們只是感情好忘晤,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著激捏,像睡著了一般设塔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上远舅,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天闰蛔,我揣著相機與錄音,去河邊找鬼图柏。 笑死序六,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蚤吹。 我是一名探鬼主播例诀,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼裁着!你這毒婦竟也來了繁涂?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤二驰,失蹤者是張志新(化名)和其女友劉穎爆土,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體诸蚕,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡步势,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了背犯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坏瘩。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖漠魏,靈堂內(nèi)的尸體忽然破棺而出倔矾,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布哪自,位于F島的核電站丰包,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏壤巷。R本人自食惡果不足惜邑彪,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望胧华。 院中可真熱鬧寄症,春花似錦、人聲如沸矩动。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽悲没。三九已至篮迎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間示姿,已是汗流浹背甜橱。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留峻凫,地道東北人。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓览露,卻偏偏與公主長得像荧琼,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子差牛,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容