<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
{{ a }}
<button @click="add('', $event)">按钮</button>
<!-- .native使组件调用构造器里面的add方法,而不是组件中的方法 -->
<Btn @click.native="add(3, $event)"></Btn>
</div>
<script src="node_modules/vue/dist/vue.js"></script>
<script>
var Btn = {
template: `
<button>按钮组件</button>
`
};
new Vue({
el: '#app',
data() {
return {
a: 0
}
},
components: {
Btn
},
methods: {
add: function (num, e) {
console.log(e);
if(num != '') {
// return this.a += num;
this.a += num;
} else {
return this.a++;
}
}
}
})
</script>
</body>
</html>