vue入门练习


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
<div id="main">
    <!--    <count-button></count-button>-->

    <combine>
        <my-title-div slot="my-title-div" v-bind:title="title"></my-title-div>
        <my-li slot="my-li" v-for="(myli,key1, index) in content"
               :key1="key1" :index="index"
               v-bind:myli="myli" @del="remove(index)"
               :key="myli"></my-li>     <!-- 与组件,和vue实例进行绑定。  -->
    </combine>
</div>
<script>
    Vue.component("combine", {
        template: "<div><slot name='my-title-div'></slot> \
                    <ul><slot name='my-li'></slot> \
                    </ul></div>"
    });
    Vue.component("my-title-div", {
        props: ["title"],
        template: "<div>{{title}}</div>"
    });
    Vue.component("my-li", {
        props: ["myli", "key1", "index"],
        template: "<li>value: {{myli}}, key1: {{key1}}, index: {{index}}<button @click='del()'>删除</button></li>",
        methods: {
            del: function (index) {
                //alert("1111")
                this.$emit("del", index); // 将前端和vue组件进行关联绑定。
            }
        }
    });
    // Vue.component
    var vm = new Vue({
        el: '#main',
        data: {
            title: "t1",
            content: ["c1", "c2"]
        },
        methods: {
            remove: function (index) {
                this.content.splice(index, 1);
            }
        }
    });

    // Vue.component("count-button", {
    //     data: function () {
    //         return {
    //             count: 0
    //         }
    //     },
    //     template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>'
    // });
</script>
</body>
</html>

版权声明:本文为weixin_44955867原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。