【vue】自定义事件

v-model自定义组件
<body>
    <div id="root">
        <base-checkbox v-model="isDone">
    </div>
</body>
import Vue from "vue";

Vue.component("base-checkbox",{
    model:{
        prop:"checked",
        event:"change"
    },
    props:{
        checked:Boolean
    },
    template:'<input type="checkbox"\
                v-bind:checked="checked"\
                v-on:change="$emit(\'change\',$event.target.checked)"\
    >',
    mounted:function(){
        console.log(this.checked);
    },
    updated:function(){
        console.log(this.checked);
    }
})

const vm = new Vue({
    el:"#root",
    data:{
        isDone:true
    }
})

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

将原生事件绑定到组件上

v-on:原生事件名称.native,将原生事件绑定到组件上。

<body>
    <div id="root">
        <base-input label="用户名" value="Nicholas" v-on:focus.native="onFocus"></base-input>
    </div>
</body>
import Vue from "vue";

Vue.component("base-input",{
    props:{
        label:String,
        value:String
    },
    template:'<label>\
            {{label}}\
                <input type="text"\
                v-bind="$attrs"\
                v-bind:value="value"\
                v-on:change="$emit(\'change\',$event.target.checked)">\
                </label>',
    methods:{
        onFocus:function(){
            console.log("now is focusing");
        }
    }
})

const vm = new Vue({
    el:"#root",
})

但是,当输入框获得焦点时,控制台并没有打印"now is focusing"这行文本。
这是因为组件base-input的根元素是<label></label>label元素上不会触发focus事件。
针对以上问题,解决方案如下:

<body>
    <div id="root">
        <base-input label="用户名" value="Nicholas"></base-input>
    </div>
</body>
import Vue from "vue";

Vue.component("base-input",{
    props:{
        label:String,
        value:String
    },
    computed:{
        inputListeners:function(){
            const self = this;
            return Object.assign({},
                this.$listeners,
                {
                    input:function(event){
                        self.$emit("input",event.target.value);
                    },
                    focus:function(){
                        console.log("focusing input");
                    }
                }
                )
        }
    },
    template:'<label>\
            {{label}}\
                <input type="text"\
                v-bind="$attrs"\
                v-bind:value="value"\
                v-on="inputListeners">\
                </label>'
})

const vm = new Vue({
    el:"#root",
})

在这里插入图片描述

sync实现双向数据绑定
<body>
    <div id="root">
        <text-component
            v-bind:title="title"
            v-on:update:title="title=$event"
        ></text-component>
    </div>
</body>
import Vue from "vue";

Vue.component("text-component",{
    props:{
        title:String
    },
    computed:{
        text:function(){
            return this.title
        }
    },
    template:'<div>\
        <span>{{text}}</span>\
        <button v-on:click="handleClick">changeTitle</button>\
    </div>',
    methods:{
        handleClick:function(){
            this.$emit("update:title","world");
        }
    }
})

const vm = new Vue({
    el:"#root",
    data:{
        title:"hello"
    }
})

在这里插入图片描述
上面的

 <text-component
     v-bind:title="title"
     v-on:update:title="title=$event"
 ></text-component>

可简写成,

<text-component v-bind:title.sync="title"></text-component>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 自定义事件可以用于在组件之间传递数据。通过在父组件中使用`$emit`方法触发自定义事件,并在子组件中使用`$on`方法监听该事件,可以实现子组件向父组件传递数据。 在Vue中,可以使用`emits`选项来声明组件支持的自定义事件。在子组件中,使用`this.$emit(eventName, data)`方法触发自定义事件,并传递数据data。父组件可以通过监听这个自定义事件来接收子组件传递的数据。 另外,还可以通过在子组件中使用`v-on`或`@`语法来绑定父组件的自定义事件,从而实现子组件向父组件传递数据。在子组件中触发自定义事件后,父组件可以通过事件处理函数来接收传递的数据。 综上所述,Vue 自定义事件是一种很方便的方式来传递数据和实现组件之间的通信。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Vue自定义事件(详解)](https://download.csdn.net/download/weixin_38641339/13624910)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Vue自定义事件](https://blog.csdn.net/qq_48731430/article/details/121779200)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Vue 自定义事件实现子组件数据向父组件传输](https://blog.csdn.net/weixin_41987908/article/details/127498540)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值