案例一:native 绑定的input事件能够触发dosomething函数app
<div id="app"> <!-- native 将原生事件绑定到组件 --> <base-input label="姓名" value="Vue" placeholder="请输入名字" v-on:input.native="dosomething"></base-input> </div> <script> Vue.component('base-input', { inheritAttrs: false, props: ['label', 'value'], template: ` <label> {{ label }} <input v-bind="$attrs" v-bind:value="value" > </label> ` }); new Vue({ el: '#app', methods: { dosomething() { console.log('y'); } } }); </script>
案例二:native 绑定的focus事件却不能够触发dosomething函数(下面案例三为解决方案)函数
<div id="app"> <!-- native 将原生事件绑定到组件 --> <base-input label="姓名" value="Vue" placeholder="请输入名字" v-on:focus.native="dosomething"></base-input> </div> <script> Vue.component('base-input', { inheritAttrs: false, props: ['label', 'value'], template: ` <label> {{ label }} <input v-bind="$attrs" v-bind:value="value" > </label> ` }); new Vue({ el: '#app', methods: { dosomething() { console.log('y'); } } }); </script>
案例三:不使用native,使用$listeners(将全部的事件监听器指向这个组件的某个特定的子元素),能够触发dosomething函数this
<div id="app"> <base-input label="姓名" value="Vue" placeholder="请输入名字" v-on:focus="dosomething"></base-input> </div> <script> Vue.component('base-input', { inheritAttrs: false, props: ['label', 'value'], template: ` <label> {{ label }} <input v-bind="$attrs" v-bind:value="value" v-on="$listeners" > </label> ` }); new Vue({ el: '#app', methods: { dosomething() { console.log('y'); } } }); </script>
案例四:从新定义$listeners,覆盖原有监听行为code
<div id="app"> <base-input label="姓名" value="Vue" placeholder="请输入名字" v-on:focus="dosomething"></base-input> </div> <script> Vue.component('base-input', { inheritAttrs: false, props: ['label', 'value'], computed: { listeners: function() { var vm = this // `Object.assign` 将全部的对象合并为一个新对象 return Object.assign({}, // 咱们从父级添加全部的监听器 this.$listeners, // 而后咱们添加自定义监听器, // 或覆写一些监听器的行为 { focus: function (event) { console.log('z'); } } ) } }, template: ` <label> {{ label }} <input v-bind="$attrs" v-bind:value="value" v-on="listeners" > </label> ` }); new Vue({ el: '#app', methods: { dosomething() { console.log('y'); } } }); </script>
总结:一切都是为了支持组件自定义原生事件component