Vue学习(9)自定义事件

本文详细介绍如何在Vue中使用自定义组件,并演示了如何通过组件调用父级的事件处理函数,实现子组件与父组件之间的数据交互。具体包括组件的创建、属性绑定、插槽使用以及事件触发和监听等核心概念。
  • 前端绑定vue
  • 前端绑定组件
  • 通过组件调用vue中的事件
<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<!--view层,模板-->
<div id="app">
    <toto>
        <toto-title slot="toto-title" :title="title"></toto-title>
        <toto-items slot="toto-items" v-for="(item,index) in totoItems"
                    :item="item" :index="index" v-on:remove="removeItems(index)"></toto-items>
    </toto>
</div>


<!--1.导入Vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script>

    Vue.component("toto",{
        template:
            '<div>\
                <slot name="toto-title"></slot>\
                <ul>\
                    <slot name="toto-items"></slot>\
                </ul>\
            </div>'
    });

    Vue.component("toto-title",{
        props: ['title'],
        template: '<div>{{title}}</div>'
    });

    Vue.component("toto-items",{
        props: ['item','index'],
        template: '<li>{{index}}-----{{item}}  <button @click="remove">删除</button> </li>',
        methods: {
            remove: function (index) {
                this.$emit('remove','index');
            }
        }
    });

    var vm = new Vue({
        el: "#app",
        data :{
            title: "列表",
            totoItems: ['狂神说JAVA','狂神说前端','狂神说Linux']
        },
        methods: {
            removeItems: function (index) {
                this.totoItems.splice(index,1);
            }
        }
    });
</script>

</body>
</html>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值