1. 程式人生 > >vue.js學習筆記(二)--指令的使用

vue.js學習筆記(二)--指令的使用

部落格地址:https://fisher-zh.github.io

vue之實現列表的新增點選。
使用指令:v-on v-for v-on v-bind v-model
html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue.js</title>
    <style>
        body {font-family: "\5FAE\8F6F\96C5\9ED1"
} .red {color: red;} body > div { width: 200px; margin: 50px auto; }
</style> </head> <body> <div id="app"> <h3>vue——列表的新增</h3> <input type="text" v-model="newitem" v-on:keyup.enter="clickKey"
>
<ul> <li v-for="item in items" v-text="item.event" v-bind:class="{red:item.isFinish}" v-on:click="liClick(item)"></li> </ul> </div> <script src="https://unpkg.com/vue/dist/vue.js"></script> <script src="testvue.js"
>
</script> </body> </html>

js

window.onload = function() {
    Vue.component("todo-item", {
        props: ["todo"],
        template: "<li>{{todo.text}}</li>"
    })
    var app = new Vue({
      el: '#app',
      data: {
        items: [
        ],
        newitem: ''
      },
      methods: {
        liClick: function (item) {
            item.isFinish = !item.isFinish;
        },
        clickKey: function () {
            this.items.push({
                event: this.newitem,
                isFinish: false
            })
            this.newitem = '';
        }
      }
    });
}