Vue.js入门到放弃Day2(中)——VueDemo -> TodoMVC

VueDemo -> TodoMVC

码云仓库地址:
https://gitee.com/chenun/todoMVC.git

Vue.js入门到放弃Day2(中)——VueDemo -> TodoMVC_第1张图片
Vue.js入门到放弃Day2(中)——VueDemo -> TodoMVC_第2张图片
index.html


<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Vue入门title>
    <link rel="stylesheet" href="node_modules/todomvc-common/base.css">
    <link rel="stylesheet" href="node_modules/todomvc-app-css/index.css">
    
    <link rel="stylesheet" href="css/app.css">
    <script src="../node_modules/vue/dist/vue.min.js">script>
head>

<body>
    
    <section class="todoapp" id="app">

        <header class="header">
            <h1>任务清单h1>
            <input class="new-todo" @keydown.enter='handleNewTodoKeyDown' placeholder="未来需要做的事是什么?">
        header>

        <template v-if="todos.length">
            <section class="main">
                <input 
                @change="handleToggleAllChange" 
                id="toggle-all" class="toggle-all" type="checkbox">
                <label for="toggle-all">全部标记为完成label>
                <ul 
               
                class="todo-list"
                >
                    <li 
                    :class='{ 
                        completed: item.completed,
                        editing: currentEditing === item
                    }'  
                    v-for='(item,index) in todos'>
                        <div class="view">
                            <input class="toggle" 
                            v-model='item.completed' 
                            type="checkbox">
                            <label 
                            @dblclick='handleGetEditingDblClick(item)'
                            >{{ item.title }}label>
                            <button 
                            @click="handleRemoveTodoClick(index)" 
                            class="destroy">button>
                        div>
                        <input 
                        @keydown.esc='handleCanleEditEsc'
                        @keydown.enter='handleSaveEditKeydown(item,index,$event)' 
                        @blur='handleSaveEditKeydown(item,index,$event)'
                        class="edit" 
                        :value="item.title"
                        >
                    li>
                ul>
            section>

            <footer class="footer">
                <span class="todo-count"><strong>0strong> item leftspan>
                <ul class="filters">
                    <li>
                        <a class="selected" href="#/">全部a>
                    li>
                    <li>
                        <a href="#/active">正在做的a>
                    li>
                    <li>
                        <a href="#/completed">已完成a>
                    li>
                ul>
                

app.js:

;
(function() {
    const todos = [{
        id: 1,
        title: '吃饭',
        completed: true
    }, {
        id: 2,
        title: '吃饭2',
        completed: true
    }, {
        id: 3,
        title: '吃饭3',
        completed: false
    }]
    new Vue({
        data: {
            todos,
            currentEditing: null
        },
        methods: {

            handleNewTodoKeyDown(e) {
                // 添加任务
                const target = e.target;
                const value = target.value.trim();

                if (!value.length) {
                    return
                }
                // console.log(value);
                const todos = this.todos;
                todos.push({
                    id: todos[todos.length - 1] + 1,
                    title: value,
                    completed: false
                });
                target.value = '';
            },

            handleToggleAllChange(e) {
                // console.dir(e.target.checked);
                const checked = e.target.checked;
                this.todos.forEach(item => {
                    item.completed = checked;
                });
            },

            // 当函数传入了参数时候,vue 不给你e点击事件了
            // 除非你传入 $event (名字固定),再函数传入参数,才可以获得 事件源对象
            // 所以这个时候我们可以手动再调用方法的时候传递 $event 来接受这个 event 事件源对象
            handleRemoveTodoClick(index) {
                // console.log(index, e);
                this.todos.splice(index, 1)
            },

            handleGetEditingDblClick(todo) {
                this.currentEditing = todo;
                // console.log(todo.title);

            },

            handleSaveEditKeydown(todo, index, e) {
                console.log(e.target.value);
                const target = e.target;
                const value = target.value.trim();
                if (!value.length) {
                    // 表示数据为空的了,直接删除
                    return this.todos.splice(index, 1);
                }
                // 保存编辑
                else {
                    todo.title = value;
                    // 移除 编辑的样式
                    this.currentEditing = null;
                }
            },

            handleCanleEditEsc() {
                this.currentEditing = null;
            },

            // 清楚已完成
            handleClearAllDoneClick() {
                // 错误的写法
                // this.todos.forEach((item, index) => {
                //     if (item.completed) {
                //         this.todos.splice(index, 1);
                //     }
                // })

                // 下面的写法二选一:

                // 所以这里要用 for 循环才可以
                for (let i = 0; i < this.todos.length; i++) {
                    if (this.todos[i].completed) {
                        this.todos.splice(i, 1);
                        // sh删除元素之后 让我们遍历的这个 小索引 往后倒退一次
                        // 因为在你删除的时候,所有元素都会往后倒退 1 个索引
                        // 纠正索引的遍历
                        i--;
                    }
                }

                // 还有一种办法,比较简单粗暴
                // this.todos = this.todos.filter(t => !t.completed);
            }
        }
    }).$mount('#app');
})();

只完成基本功能

你可能感兴趣的:(Vue与Node学习,vue)