五、Vue之Enter键触发事件

//@keydown 键盘按下事件
<input type="text" v-model="todo" @keydown="addByEnterKey" />
<script>
export default {
    name: 'app',
    data() {
        return {
            todo: '',
            list: []
        }
    },
    methods: {
        addByEnterKey(e) {
	        //Enter键的代码就是13
            if (e.keyCode == 13) {
              this.add();
            }
        },
        add() {

            var obj = {
                title: this.todo,
                isCompleted: false
            };
            this.list.push(obj);
            this.todo = '';
        },
        remove(key) {
            this.list.splice(key, 1);
        }
    }
}
script>

你可能感兴趣的:(vue)