vue之事件和小案例实现TODO

2019-03-02  本文已影响0人  蜗牛ICU
一 、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>
上一篇下一篇

猜你喜欢

热点阅读