美文网首页
vue之事件和小案例实现TODO

vue之事件和小案例实现TODO

作者: 蜗牛ICU | 来源:发表于2019-03-02 18:58 被阅读0次
    一 、vue 支持触发事件:

    写法是:
    v-on:click="fn" 等价于 @click="fn" 等价于

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>事件</title>
    </head>
    <body>
        <div id="app">
            <div v-on:click="fn">
                点我啊, 小哥哥!
            </div>       
        </div>
        <script src="./node_modules/vue/dist/vue.js"></script>
    <script>
        let vm = new Vue({
            el:"#app",
            data:{},
            methods:{
                fn(){alert(1)}
            }
        })    
    </script>
    
    </body>
    </html>
    
    二、TODO:
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>TODO</title>
    </head>
    
    <body>
        <div id="app">
            <input type="text" v-model="val" @keyup="add" />
            <ul>
                <li v-for="(a,index) of arr">
                    {{a}}<button @click="remove(index)">删除</button>
                </li>
            </ul>
        </div>
        <script src="./node_modules/vue/dist/vue.js"></script>
        <script>
            let vm = new Vue({
                el: "#app",
    
                methods: {
                    add(e) {
                        if (e.keyCode === 13) {
                            console.log(this.val);
                            this.arr.push(this.val);
                            this.val = '';
                        }
    
                    },
                    remove(i){
                            this.arr=this.arr.filter((item,index)=>index!==i)
                    }
                },
                data: {
                    val: '',
                    arr: [],
    
                }
            })    
        </script>
    
    </body>
    
    </html>
    

    相关文章

      网友评论

          本文标题:vue之事件和小案例实现TODO

          本文链接:https://www.haomeiwen.com/subject/xwkyuqtx.html