牛骨文教育服务平台(让学习变的简单)
博文笔记

使用Vue.js完成一个todo-list

创建时间:2017-05-11 投稿人: 浏览次数:1831

使用Vue.js完成一个todo-list

首先分析一下页面的组成,要与一个供输入的用户输入框,然后下面应该是个列表,这个列表是用户的输入的内容的展示

页面结构:

    <div id="todo-list-emp">
      <input type="text" placeholder="Add a todo" v-model="newTodoText" v-on:keyup.enter="addNewTodo">
      <ul>
        <li is="todo-item" v-for="(todo, index) in todos" v-bind:title="todo" v-on:remove="todos.splice(index, 1)">
        </li>
      </ul>
    </div>

JavaScript部分:

        Vue.component("todo-item", {
           template : "<li>{{ title }}<button v-on:click="$emit("remove")">X</button></li>",
            props: ["title"]
        });
        new Vue({
            el : "#todo-list-emp",
            data : {
                newTodoText : "",
                todos : [
                    "Do the dishes",
                        "Take out the trash",
                        "Mow the lawn"
                ]
            },
            methods: {
                addNewTodo : function() {
                    this.todos.push(this.newTodoText);
                    this.newTodoText = "";
                }
            }
        });

基本的样式:

        input {
            width: 300px;
            height: 30px;
            border: 2px solid #DDDDDD;
            border-radius: 5px;
            font-size: 16px;
            color: rgba(0, 0, 0, 0.6);
            text-align: center;
        }
        input:hover {
            border: 2.5px solid #42B983;
            color: rgba(0, 0, 0, 0.7);
        }
        ul {
            padding: 0;
            margin-top: 15px;
        }
        ul li {
            width: 300px;
            height: 40px;
            line-height: 40px;
            color: rgba(0, 0, 50, 0.8);
            font-size: 22px;
            font-family: "Lato", Calibri, Arial, sans-serif;
            list-style: none;
            border: 1px solid #333333;
            border-radius: 5px;
            text-align: center;
        }
        ul li + li {
            margin-top: 1px;
        }
        li button {
            font-size: 8px;
            line-height: 14px;
            background: #fff;
            border: 0;
            float: right;
            margin-right: 3px;
            color: rgba(0, 0, 0, 0.9);
        }

代码下载链接:http://download.csdn.net/detail/dear_mr/9839795

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。