1,给vue组件绑定事件时候,必须加上native ,否则会认为监听的是来自Item组件自定义的事件
2,等同于在子组件中: 子组件内部处理click事件然后向外发送click事件:$emit("click".fn)
补充知识:vue——组件间(兄弟组件间)事件派发与接收
法一
main.js
在初始化vue之前,给 data 添加一个名为 event 的空vue对象
new Vue({
render: h => h(App),
router,
store,
data: {
event: new Vue()
}
}).$mount('#app')
组件一:
methods: {
addCart (e) {
let pos = {
x: parseInt(e.target.getBoundingClientRect().x + 4),
y: parseInt(e.target.getBoundingClientRect().y + 4)
}
this.$root.event.$emit('ballPosition', pos)
}
}
组件二:
created () {
this.$root.event.$on('ballPosition', (target) => {
this._initBall(target)
})
},
methods: {
_initBall (target) {
this.ball = true
this.ballMassage = target
}
}
完整案例:
抛物小球动画:
created () {
this.$root.event.$on('ballPosition', (target) => {
this._initBall(target)
})
},
methods: {
_initBall (el) {
this.ball.show = true
this.ball.el = el
},
beforeEnter (el) {
let pos = this.ball.el.target.getBoundingClientRect()
el.style.top = `${pos.y}px`
el.style.left = `${pos.x}px`
},
enter (el, done) {
// 触发动画重绘
el.offsetHeight
let [x, y] = [parseInt(this.$refs.cart.getBoundingClientRect().x + 4), parseInt(this.$refs.cart.getBoundingClientRect().y + 8)]
el.style.top = `${y}px`
el.style.left = `${x}px`
el.style.transition = `left .1s linear, top .1s cubic-bezier(.63,.02,.96,.56)`
done()
},
afterEnter () {
this.ball.show = false
}
},
法二
中央通信: let eventVue = new Vue()
A methods:{function(){eventVue.$emit('自定义事件','数据')}}
B created(){eventVue.$on('A 发送过来的事件名','函数')}
中央通信:
let eventVue = new Vue()
兄弟组件 A 如下:
兄弟组件 B 如下:
{{btext}}
以上这篇vue组件添加事件@click.native操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。



