Vue 简单的组件间传值(五)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38287952/article/details/88088032
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <input type="text" v-model="inputValue">
        <button @click="handleClick">提交</button>
        <ul>
            <todo-item @content="item"
                       @index="index"
                       v-for="(item, index) in list"
                       @delete="handleItemDelete"
            ></todo-item>
        </ul>
    </div>
    <script>

        // 全局的组件
        /*Vue.component("TodoItem", {
            props: ['content'],
            template: "<li>{{content}}</li>"
        });*/

        // 局部组件
        var TodoItem = {
            props: ['content', 'index'],
            template: "<li @click='handleItemClick'>{{content}}</li>",
            methods: {
                handleItemClick: function () {
                    this.$emit('delete', this.index); // 向外出发事件
                }
            }
        }

        var app = new Vue({
            el: '#app',
            components: {
                TodoItem: TodoItem
            },
            data: {
                list: [],
                inputValue: ''
            },
            methods: {
                handleClick: function () {
                    this.list.push(this.inputValue);
                    this.inputValue = '';
                },
                handleItemDelete: function (index) {
                    this.list.splice(index, 1);
                }
            }
        });
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_38287952/article/details/88088032