<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>组件绑定原生事件</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<body>
<div id="app">
<child @click="handleClick"></child>
<child @click.native="handleClick2"></child>
</div>
<script>
Vue.component('child', {
template: "<div @click='handleChildClick'>child</div>", //监听事件
methods: {
handleChildClick: function() {
alert('handleChildClick');
this.$emit('click') //自定义事件触发
}
}
})
var vm = new Vue({
el: '#app',
methods: {
handleClick: function() {
alert('handleClick');
},
handleClick2: function() {
alert('handleClick2');
// 方法二不用$emit 加native修饰符即可
}
}
})
</script>
</body>
</html>
网友评论