vue -- 實(shí)例 - 記事本

新增

  • 生成列表結(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)計(jì)和清空 -->
        <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>

刪除

  • 點(diǎn)擊刪除指定內(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)計(jì)和清空 -->
        <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)計(jì)和清空

  • 統(tǒng)計(jì)信息個(gè)數(shù)(v-text length)
  • 基于數(shù)據(jù)的開發(fā)方式
  • 點(diǎn)擊清空所有信息(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)計(jì)和清空 -->
        <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ù)時(shí) 隱藏元素(v-show v-if 數(shù)組非空)
  • 列表結(jié)構(gòu)可以通過v-for指令結(jié)合數(shù)據(jù)生成
  • v-on結(jié)合事件修飾符可以對(duì)事件進(jìn)行限制 比如.enter
  • v-on在綁定事件時(shí) 可以傳遞自定義參數(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)計(jì)和清空 -->
        <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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Vue學(xué)習(xí)筆記 一、環(huán)境搭建 1、安裝VS code 2、安裝live server 3、安裝科學(xué)上網(wǎng)工具 4、安...
    Daydream_許多閱讀 1,016評(píng)論 0 0
  • 1.安裝 可以簡(jiǎn)單地在頁面引入Vue.js作為獨(dú)立版本,Vue即被注冊(cè)為全局變量,可以在頁面使用了。 如果希望搭建...
    Awey閱讀 11,298評(píng)論 4 129
  • Vue安裝 安裝腳手架,使用全局安裝就可以 安裝完使用這個(gè)命令查看vue cli的版本 初始化一個(gè)Vue項(xiàng)目 然后...
    愛吃胡蘿卜的小白兔閱讀 247評(píng)論 0 0
  • 分享一個(gè)用vue做的一個(gè)案例,這個(gè)案例結(jié)合了大多的vue的入門知識(shí),包括有點(diǎn)擊事件,數(shù)據(jù)雙向綁定,v-for獲取...
    飛要好好學(xué)前端啊閱讀 1,503評(píng)論 0 1
  • 1.vue的新建: (1)在head添加 來添加vue庫。 (2)在下邊的body中再添加 var vm =vu...
    d48864d6c1b5閱讀 361評(píng)論 0 0

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